0

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?

1
  • So you're trying to loop througheach item in the model and add that item to the array? Commented Oct 16, 2015 at 18:24

2 Answers 2

1

Try

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

Sign up to request clarification or add additional context in comments.

1 Comment

This didn't work, I had to call an action in the controller and pass back JSON data.
0

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++;
}

1 Comment

This didn't work, I had to call an action in the controller and pass back JSON data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.