我试图在视图上生成两组复选框。除Post事件之外,这一切都是工作。提交时, ParentViewModel未正确绑定ChildViewModel 模型。 FirstCheckboxList Model。 SecondCheckboxList 以上都是空的。@ Html.Editor模型绑定问题
我不知道我错过了什么。任何对此的帮助都会很大。 在此先感谢。
CheckboxItems.cshtml
@model List<CheckboxItem>
@{
for (int i = 0; i < Model.Count(); i++)
{
<div>
@Html.CheckBoxFor(x => x.ElementAt(i).Checked, new { @id = Model.ElementAt(i).Id, onclick = "GetValue()" })
<span id="Padded">@Model.ElementAt(i).Text</span>
</div>
}
}
MainView.cshtml
@Html.BeginForm(){
@Html.EditorFor(m=> m.FirstCheckboxList,"CheckboxItems")
@Html.EditorFor(m=> m.SecondCheckboxList, "CheckboxItems")
}
@Html.TextBoxFor(m => m.FSelected, new Dictionary<string,object>() {{"readonly",true}})
@Html.TextBoxFor(m => m.FUniverse,new Dictionary<string,object>() {{"readonly",true}})
<input type="submit" name="nextBtn" value ="Next" />
}
ParentViewModel
public class ParentViewModel
{
public int PId { get; set; }
public IEnumerable<CheckboxItem> FirstCheckboxList{ get; set; }
public IEnumerable<CheckboxItem> SecondCheckboxList{ get; set; }
public Int64 FSelected { get; set; }
public Int64 FUniverse { get; set; }
}
CheckboxItem:子视图模型
public class CheckboxItem
{
public int Id { get; set; }
public string Text { get; set; }
public bool Checked { get; set; }
}
控制器动作
[HttpPost]
public ActionResult MyCheckboxView(int planid, ParentViewModel model, string nextBtn)
{
// do something
return View(Model);
}
复选框的名字应该是这样的firstchecboxlist [i] .fieldname ..查看所呈现的html – maxlego 2013-04-29 16:00:27