简体   繁体   中英

How to build javascript array for google map coordinates using MVC Model

I'm trying to build an array using the data from an MVC Model

    var locations = [];

I'm trying to loop through the Model and build the array like this:

     @{var count = 0; }
    @foreach (var item in Model.Locations)
    {
    locations[count] = new locations[@item.StreetAddress, @item.Latitude, @item.Longitude, count+1];
    count++;
    }

Any ideas on how to get this done?

尝试

var locations = @Html.Raw(Json.Encode(Model.Locations));

If I understand correctly you are trying to loop through each item and add to array? Why not just cast your list to an array? I'm assuming your Model.Locations is a list of Location

var locations = Model.Locations.ToArray();

There your list is cast to an array in 1 line. If your Model.Locations is not a list or you want to step through each item one by one for other reasons :

@{var count = 0; }
@foreach (var item in Model.Locations)
{
    locations[Model.Locations.IndexOf(item)] = new location[@item.StreetAddress, @item.Latitude, @item.Longitude, count+1];
    count++;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM