ViewModel collection property lost values after posting back to controller action in MVC 3

余生颓废 提交于 2019-12-05 03:30:18

try

@for (int i = 0; i < Model.POReceiptItems.Count(); i++)
{ 
<tr>
  <td>@Html.DisplayTextFor(m => m.POReceiptItems[i].ItemCode)</td>@Html.HiddenFor(m => m.POReceiptItems[i].ItemCode)    
  <td>@Html.DisplayTextFor(m => m.POReceiptItems[i].ItemDesription)</td>@Html.HiddenFor(m => m.POReceiptItems.ItemDesription)                                                               <td>@Html.DisplayTextFor(m => m.POReceiptItems[i].OrderedQuantity)</td>@Html.HiddenFor(m => m.POReceiptItems[i].OrderedQuantity)  
  <td>@Html.TextBoxFor(m => m.POReceiptItems[i].ReceivedQuantity)</td>
  <td>@Html.TextBoxFor(m => m.POReceiptItems[i].ReceivedDate)</td>
</tr>
}

also read this blog post to understand how model binding to a list works

You lose your list because MVC don't handle the List the way you think.

You should use BeginCollectionItem look at this post

I had a similar problem, the "List" attribute returned without values(count = 0), I tried different ways and answers and nither works. Then I tried by myself and now it is working, this is my solution:

I send an object with some normal attributes and a "List", after that I used the normal attributes and my "list" in a For. In my controller (Post ActionResult), in the parameters section I added two parameters, my original object and my "List" as second parameter and it works!!! I hope this helps you and others with similar problems.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!