.NET MVC : Pass back complex object or list array from view to controller

落花浮王杯 提交于 2019-12-05 19:48:39

You can pass back a list within a view model using Html.hidden for each element of a list.

The list property in your view model will be re-constructed as long as you process the list elements using a for loop in your view (foreach will not work). For example:

@for (var i = 0; i < Model.Nutrients.Count(); i++) 
{
  // This ensures that the list of nutrients is passed in the view model back to the controller
  @Html.HiddenFor(m => m.Nutrients[i].Name);
  @Html.HiddenFor(m => m.Nutrients[i].Id);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!