In MVC, is it possible to pass an array of string (List<string>) from C# code to viewdata and then retrieve the value in the view? e.g:
C#:
List<string> names = new List<string>();
names.Add("a");
names.Add("b");
ViewData["names"] = names;
MVC view:
<script type="text/javascript">
var nameList = '<%= ViewData["names"] %>';
</script>
the code above runs OK but the returned javascript object in the view is “System.Collections.Generic.List`1[System.String]” which cannot be parsed to get the values out.
What is the right way to achieve that rather than build the string with delimiter in C# and split the value in the view?
Thanks!
You could use JavaScriptSerializer to generate the JS representation of your data.