Pass Array from MVC to javascript?

前端 未结 9 1083
难免孤独
难免孤独 2020-11-28 23:32

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

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

This work fine

9条回答
  •  难免孤独
    2020-11-29 00:12

    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));

提交回复
热议问题