@Html.HiddenFor does not work on Lists in ASP.NET MVC

后端 未结 13 935
灰色年华
灰色年华 2020-11-27 15:13

I\'m using a model that contains a List as a property. I\'m populating this list with items i grab from SQL Server. I want the List to be hidden in the view and passed to th

13条回答
  •  一向
    一向 (楼主)
    2020-11-27 15:44

    Faced the same issue. Without for loop, it only posted the first element of the list. After iterating through for loop, it can keep full list and post successfully.

     @if (Model.MyList!= null)
        {
        for (int i = 0; i < Model.MyList.Count; i++)
          {
            @Html.HiddenFor(x => x.MyList[i])
          }
        }
    

提交回复
热议问题