MVC3 ModelBinding to a collection posted back with index gaps

前端 未结 2 1953
情话喂你
情话喂你 2021-01-13 03:26

I have a collection of objects on my Model that I\'m rendering in a View by using EditFor function, and I have an EditorTemplate which is responsible for actually rendering

相关标签:
2条回答
  • 2021-01-13 04:05

    There are some very good blog posts that allow you to modelbind to a list without the need to provide zero based contiguous index. plz have a look at http://blog.stevensanderson.com/2010/01/28/editing-a-variable-length-list-aspnet-mvc-2-style/
    http://zahidadeel.blogspot.com/2011/05/master-detail-form-in-aspnet-mvc-3-ii.html
    Furthermore, if you are interested in MVVM pattern and knockout js you can check this great work by steve sanderson
    For more reading put "editing varibale length list mvc style" in google and it will give u a dozen useful links

    0 讨论(0)
  • 2021-01-13 04:18

    Ran into this issue recently and solved it by converting the List to a Dictionary<string, model> with GUIDs as the key.

    @foreach (var index in Model.EmailAddresses.Keys)
    {
        <label asp-for="@Model.EmailAddresses[index].Email">Email</label>
        <input asp-for="@Model.EmailAddresses[index].Email" type="text" />
    }
    

    This avoided having to include hidden inputs that map to the index value.

    0 讨论(0)
提交回复
热议问题