我已經研究過所有的網絡,但希望在這裏有人可以幫助我。ASP.NET MVC5 Model Binder - 綁定集合集合時爲空
我有以下視圖模型類:
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; }
}
在編輯視圖我使用自定義EditorTemplate使用@Html.EditorFor(m => m.Interests)
興趣的佈局集合。有兩個EditorTemplates是做渲染:
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)
問題:
使用POST提交表單時,只有Interests
集合會被填充,並且Interest.Items
集合始終爲空。 請求包含(除其他外)以下字段名稱,它們在檢查控制器操作方法中的Request.Forms數據時也存在。
- 興趣[0] .ID
- 興趣[0]請將.Name
- 興趣[0] .Items [0] .ID
- 興趣[0] .Items [0] .Selected
所有包含適當的值 - 但在控制器側中,對象在PVM方法:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(PersonEditViewModel pvm)
{
}
包含Interests集合中的數據(具有正確ID和名稱的項目),但是對於集合的每個元素,其'Item的子集合爲空。
我該如何正確選擇模型?
不是很熟悉MVC,但也許你的模型必須是[Serializable接口] – Suhan