How to iterate over @Model.ICollection in Java Script function?

613 views Asked by At

I have a @Model.LoginCoordinates where LoginCoordinates is ICollection<Coordinates>.

The Coordinates class:

public class Coordinates {
    public int Id { get; set; }
    public double Latitude { get; set; }
    public double Longitude { get; set; }
}

Inside the view in I have:

<script>
    function f(){
       for (var i = 0, n  = @Model.LoginCoordinates.Count; i < n; i++) {
                    alert(i);//works fine, I just wanted to check if the loop works at all
       }
    }
</script>

It works fine, but instead of that I would like to display all the do:

 <script>
     function f(){
          for (var i = 0, n  = @Model.LoginCoordinates.Count; i < n; i++) {
                  var latitute = @Model.LoginCoordinates[i].Latitude;
                  var longituted = @Model.LoginCoordinates[i].Longitude;
         }
     }
</script>

But I cannot access i element of the LoginCoordinates because it is ICollection. Also I believe foreach is impossible at it is c# object.

Question: how to iterate over ICollection inside JavaScript?

EDIT

This above is SSCCE, the real code is;

  var map;
    function InitializeMap() {
        alert('works');
        var lat = @Model.LoginCoordinates.First().Latitude;
        var lon = @Model.LoginCoordinates.First().Longitude;
        var latlng = new google.maps.LatLng(lat, lon);
        var mapOptions =
        {
            zoom: 16,
            center: latlng,
            mapTypeId: google.maps.MapTypeId.ROADMAP,
            disableDefaultUI: true
        };
        map = new google.maps.Map(document.getElementById("map"), mapOptions); //this breaks the script

    }
    window.addEventListener('load', InitializeMap);
4

There are 4 answers

3
area28 On BEST ANSWER

This should do it using JsonConvert from newtonsoft.json

<script>
var coordinatesJson='@Html.Raw(JsonConvert.Serialize(Model.LoginCoordinates.ToArray())'

var coordinates=JSON.parse(coordinatesJson);

//you now have coordinates as javascript  object

var map;
function InitializeMap() {
    // could loop over if needed
     for(var coords in coordinates) {
        // do something with coords
     }
</script>
3
Gurpreet On

How about

<script>
 function f(){
     var coordinates= @Model.LoginCoordinates.ToArray();
     for (var i = 0, n  = coordinates.Length; i < n; i++) {
              var latitute = coordinates[i].Latitude;
              var longituted = coordinates[i].Longitude;
     }
 }
</script>
0
James R. On

The Model is only accessible on the server-side as the view is being rendered. Therefore, in order to access its properties on the client-side, you would need to iterate over the collection in C# (razor) and generate HTML that the client-side JavaScript code could then access.

Another alternative would be to create a controller method that returns the model as JSON and call it from the client-side code via AJAX.

0
TSmith On

For what it's worth, this syntax may be useful to someone:

      @section Scripts {
      <script type="text/javascript">  
        @if (Model.LoginCoordinates.Any())
        {
            //C# loop over C# object
            foreach (var point in Model.LoginCoordinates)
            {
                //We are mixing C# and JS code here
                //you could call google's JS API, but still work with C# object properties
                @:console.log(@point.Latitude, @point.Longitude);
            }
        }
      </script>
     }