简体   繁体   English

ASP.NET MVC5模型绑定器-绑定集合的集合时为Null

[英]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:

  1. DictionaryRootViewModel.cshtml: DictionaryRootViewModel.cshtml:

     @model Platforma.Models.DictionaryRootViewModel @Html.HiddenFor(model => model.Id) @Html.HiddenFor(model => model.Name) @Html.EditorFor(model => model.Items) 
  2. 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数据时,它们也存在。

  • Interests[0].Id 利益[0] .ID
  • Interests[0].Name 利益[0]请将.Name
  • Interests[0].Items[0].Id 利益[0] .Items [0] .ID
  • Interests[0].Items[0].Selected 利益[0] .Items [0] .Selected

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.

相关问题 在ASP.NET MVC4模型绑定器中将ViewModels与动态嵌套集合绑定 - Binding ViewModels with Dynamic Nested Collections in ASP.NET MVC4 Model Binder ASP.NET MVC5:想要使用模型绑定更新Collection中的多个项目 - ASP.NET MVC5: Want to update several items in Collection with model binding ASP.NET MVC5-可空模型属性在编辑模型时始终丢失其值(空) - ASP.NET MVC5 - Nullable model property always losing its value (null) when editing model 为什么在ASP.NET MVC5中使用model-binder进行数据注释不起作用? - why Data Annotation with model-binder in asp.net MVC5 not working? ASP.NET模型绑定器无法绑定包含集合的复杂对象集合 - ASP.NET Model binder cannot bind collection of complex of object containing collections 在 ASP.NET MVC 中的自定义模型绑定器中绑定属于另一个对象的列表 - Binding a list belonging to another object in a custom model binder in ASP.NET MVC ASP.NET Core MVC 中的模型绑定与集合 - Model Binding in ASP.NET Core MVC with Collection ASP.NET MVC / Web API模型绑定项到集合 - ASP.NET MVC/Web API model binding item to collection asp.net mvc 6模型绑定到复杂集合--IList <T> - asp.net mvc 6 model binding to complex collection - IList<T> asp.net mvc modelbinding没有绑定集合 - asp.net mvc modelbinding not binding collections
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM