7

Is it possible to convert a MVC C# List to an javascript array?

var jsArray = @Model.IntList;

Any help would be greatly appreciated.

3

2 Answers 2

8

You can do

<script type="text/jscript">
  var jsonVariable = @(Html.Raw(Newtonsoft.Json.JsonConvert.SerializeObject(Model.IntList)));//using Newtonsoft
//or you can use below
//var jsonVariable = @Html.Raw(Json.Encode(Model.IntList))
</script>
Sign up to request clarification or add additional context in comments.

1 Comment

For future, if you need some json data, take a look to msdn.microsoft.com/en-us/library/… and msdn.microsoft.com/en-us/library/…
2

You can use below approach for convert @Model.IntList to javascript array

<script type="text/jscript">
  var array = @Html.Raw(Json.Encode(Model.IntList))
</script>

1 Comment

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.