How do I set a Javascript variable to a Model string array in MVC3?
Model.Choices is a string array in my MVC3 view. My question is, how can I get a JavaScript string array from it? If you do something like:
var开发者_运维知识库 choices="@Model.Choices";
alert(choices);
alert(choices[1]);
The message will be "choice1choice2choice3choice4" and "undefined"
What is the syntax I need to have to have choices turn into a string array in JavaScript? For example:
alert(choices[0]);
alert(choices[1]);
Should output "choice1", and "choice2".
var choices = [ '@Html.Raw(string.Join("', '", Model.Choices))' ];
Please note, if you have 0 items, this will fail by adding one empty item to the array. You should ensure that the choices won't have any '
(single quotes).
['@string.Join("','", Model.Choices)']
精彩评论