javascript object System.Collections.Generic.List`1[System.String]?
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.Strin开发者_JS百科g]" 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.
<script type="text/javascript">
var nameList = <%= new JavaScriptSerializer().Serialize(ViewData["names"]) %>;
</script>
精彩评论