[英]ASP.NET MVC5 Model Binder - Null when binding collection of collections
I have researched all over the net but hopefully here someone can help me. 我已经在网上进行了广泛的研究,但希望这里有人可以帮助我。
I have following ViewModel classes: 我有以下ViewModel类:
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 the Edit view I use custom EditorTemplate to layout collection of Interests using @Html.EditorFor(m => m.Interests)
. 在“编辑”视图中,我使用自定义EditorTemplate通过@Html.EditorFor(m => m.Interests)
来布局兴趣集合。 There are two EditorTemplates that do the rendering: 有两个执行渲染的EditorTemplates:
DictionaryRootViewModel.cshtml: DictionaryRootViewModel.cshtml:
@model Platforma.Models.DictionaryRootViewModel @Html.HiddenFor(model => model.Id) @Html.HiddenFor(model => model.Name) @Html.EditorFor(model => model.Items)
DictionaryItemViewModel.cshtml: 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 the form using POST, only the Interests
collection gets populated and the Interest.Items
collection is always empty. 当使用POST提交表单,只有Interests
收集被填充和Interest.Items
集合总是空的。 The request contains (among others) following field names, they are also present when inspecting Request.Forms data in the controller action method. 该请求包含(除其他外)以下字段名称,在控制器操作方法中检查Request.Forms数据时,它们也存在。
All contain proper values - but on the controller side, the object pvm in method: 全部包含正确的值-但在控制器端,方法中的对象pvm:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(PersonEditViewModel pvm)
{
}
contains data in Interests collection (items with correct id and name), but for each element of the collection its' subcollection of Items is empty. 包含“兴趣”集合中的数据(具有正确ID和名称的项目),但是对于集合中的每个元素,其“子项”都是空的。
How do I make it to correctly pick the model? 如何使其正确选择模型?
As usual - the answer was in front of me the whole time. 像往常一样-答案一直都在我眼前。 The DefaultModelBinder didn't pick up the Items values passed in request, because I "forgot" to mark Items collection as property - it was a field! DefaultModelBinder没有拾取在请求中传递的Items值,因为我“忘记”将Items集合标记为属性-这是一个字段! The correct form taking into account helpful remark by @pjobs: 考虑到@pjobs的有用评论的正确形式:
public List<DictionaryItemViewModel> Items
{get;set;} public List<DictionaryItemViewModel> Items
{get; set;}
Most of the time issue is with index, when you have collections posting, you either needs to have sequential index or have Interests[i].Items.Index hidden field if indexes are not sequential. 大多数时候都是与索引有关的,当您发布馆藏时,您需要具有顺序索引,或者如果索引不是顺序的,则需要有Interests [i] .Items.Index隐藏字段。
Here is similar question on SO 这是类似的问题
It will not work if you have 如果你有它不会工作
Interests[0].Id
Interests[0].Name
Interests[0].Items[0].Id
Interests[0].Items[0].Selected
Interests[0].Items[2].Id
Interests[0].Items[2].Selected
So to fix it you either make sure have sequencial indexes as 因此,要解决此问题,您要么确保将序列索引设置为
Interests[0].Id
Interests[0].Name
Interests[0].Items[0].Id
Interests[0].Items[0].Selected
Interests[0].Items[1].Id
Interests[0].Items[1].Selected
OR 要么
Interests[0].Id
Interests[0].Name
Interests[0].Items.Index = 0 (hidden field)
Interests[0].Items[0].Id
Interests[0].Items[0].Selected
Interests[0].Items.Index = 2 (hidden field)
Interests[0].Items[2].Id
Interests[0].Items[2].Selected
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.