ASP.NET MVC5: Want to update several items in Collection with model binding

前端 未结 1 1670
一向
一向 2021-02-15 16:24

So I have a Collection of User objects, which should be mass-editable (edit many users at the same time). I\'m saving the user input to the database using Entity Framework.

相关标签:
1条回答
  • 2021-02-15 17:22

    You need to index your collection with a for rather than a foreach in order for the ModelBinder to pick it up:

    for (var i = 0 ; i < Model.Count(); i++)
        {
            @Html.HiddenFor(modelItem => modelItem[i].Id)
            @Html.EditorFor(modelItem => modelItem[i].FirstName)
            @Html.ValidationMessageFor(modelItem => modelItem[i].FirstName)
            @Html.EditorFor(modelItem => modelItem[i].LastName)
            @Html.ValidationMessageFor(modelItem => modelItem[i].LastName)
            @Html.EditorFor(modelItem => modelItem[i].Birth)
            @Html.ValidationMessageFor(modelItem => modelItem[i].Birth)
        }
    
    0 讨论(0)
提交回复
热议问题