Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I can pass a variable from MVC ASP.NET by using this :

var lastCategoryId = '<%=Model.CS.LastSelectedCategory %>';

This work fine with string or integer but how do I do with an array of strings? I have tried to pass the array the same way but the variable is set to System.String[] ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
412 views
Welcome To Ask or Share your Answers For Others

1 Answer

You could let .NET handle all the heavy lifting for you with this simple line of code.

This assumes you're using MVC Razor syntax.

var yourJavaScriptArray = @Html.Raw(Json.Encode(Model.YourDotNetArray));

For newer versions of MVC, use:

var yourJavaScriptArray = @Html.Raw(Json.Serialize(Model.YourDotNetArray));


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...