Pass Array from MVC to javascript?

前端 未结 9 1062
难免孤独
难免孤独 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:17

    You need to format the array into a JavaScript array syntax.

    var someArray = [<%= Model.SomeArray.Select(x => "'" + x +"'")
                               .Aggregate((x,y) => x + ", " + y);  %>];
    

    This will surround each entry by single quotes and then join them together with commas between square brackets.

    Updated: removed extra parenthesis.

提交回复
热议问题