ASP.NET MVC 开发中一些常见且容易忘记的代码摘要(不定期更新)

1.@Html.Raw(Json.Encode(Model))

Controller 端return view(model) 之后, 想再jquery的 ready方法中直接使用 (比如 @Model.UserList),需要做一下转换,将C#集合转为 js的数组.

注: 包括像Vue的 data 内部数组的绑定赋值,也是如此转换完之后直接赋值即可使用。

Demo:

JS:

$(function(){

  var productList = @Html.Raw(Json.Encode(Model.ProductList));

})

 

C#:

public ActionResult Index()

{

  var model = new IndexModel(){ ProductList = new List<XXX>() };

        return View(model );

}

posted @ 2018-04-03 11:04  死神的背影  阅读(64)  评论(0)    收藏  举报