3

My Model lokes something like this

public class ResponseData
{
    public List<Item> Items {get; set;}
    public string Name {get; set;}
}

I know that I can bind Name to JavaScript in my view(.cshtml) like this: var jsName = '@Model.Name'

The question is how do I bind List to an array? I want to do something like this

var Array = @Model.Itmes and then be able to iterate this array via JavaScript.

Thanks you!

1 Answer 1

6

Mate's answer would work. Another option to turn your model into a JSON object:

<script type="text/javascript">
    var model = @Html.Raw(Json.Encode(Model));
    var items = model.Items;
</script>
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you this is exactly what I was looking for. By the way I don't think that you should have semicolon after @Html.Raw(Json.Encode(Model))

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.