c# - ASP.NET MVC5 Model Binder - Null when binding collection of collections -
i have researched on net here can me.
i have following viewmodel classes:
public class personeditviewmodel { public person person { get; set; } public list<dictionaryrootviewmodel> interests { get; set; } } public class dictionaryrootviewmodel { public long id { get; set; } public string name { get; set; } public icollection<dictionaryitemviewmodel> items; public dictionaryrootviewmodel() { items = new list<dictionaryitemviewmodel>(); } } public class dictionaryitemviewmodel { public long id { get; set; } public string name { get; set; } public bool selected { get; set; } }
in edit view use custom editortemplate layout collection of interests using @html.editorfor(m => m.interests)
. there 2 editortemplates rendering:
dictionaryrootviewmodel.cshtml:
@model platforma.models.dictionaryrootviewmodel @html.hiddenfor(model => model.id) @html.hiddenfor(model => model.name) @html.editorfor(model => model.items)
dictionaryitemviewmodel.cshtml:
@model platforma.models.dictionaryitemviewmodel @html.hiddenfor(model => model.id) @html.checkboxfor(model => model.selected) @html.editorfor(model => model.name)
the problem:
when submitting form using post, interests
collection gets populated , interest.items
collection empty. request contains (among others) following field names, present when inspecting request.forms data in controller action method.
- interests[0].id
- interests[0].name
- interests[0].items[0].id
- interests[0].items[0].selected
all contain proper values - on controller side, object pvm in method:
[httppost] [validateantiforgerytoken] public actionresult edit(personeditviewmodel pvm) { }
contains data in interests collection (items correct id , name), each element of collection its' subcollection of items empty.
how make correctly pick model?
as usual - answer in front of me whole time. defaultmodelbinder didn't pick items values passed in request, because "forgot" mark items collection property - field! correct form taking account helpful remark @pjobs:
public list<dictionaryitemviewmodel> items
{get;set;}
Comments
Post a Comment