2013-04-29 84 views
0

我试图在视图上生成两组复选框。除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); 
    } 
+1

复选框的名字应该是这样的firstchecboxlist [i] .fieldname ..查看所呈现的html – maxlego 2013-04-29 16:00:27

回答

1

试着改变你的视图模型为ParentViewModel使用的List<CheckboxItem>而不是IEnumerable<CheckboxItem>

public class ParentViewModel 
{  
    public int PlanId { get; set; } 
    public List<CheckboxItem> FirstCheckboxList{ get; set; } 
    public List<CheckboxItem> SecondCheckboxList{ get; set; } 
    public Int64 FSelected { get; set; } 
    public Int64 FUniverse { get; set; } 
} 

模型绑定需要像ListArray,这样的数据结构它可以在指定索引处正确绑定元素。 IEnumerable只是一个接口,不支持像这样的索引。

编辑

此外,作为一个侧面说明,你不必在你的EditorTemplate的for循环打扰,因为MVC可以做到这一切为您服务。只要改变模型类型为@model CheckboxItem,消除环路和摆脱id属性的,所以它看起来是这样的:

@model CheckboxItem 
@{   

    <div> 
    @Html.CheckBoxFor(x => x.Checked, new { onclick = "GetSelectedFrame()" }) 
     <span id="Padded">@Model.Text</span> 
    </div> 
    } 
} 

此外,请确保您的EditorFor调用不会提供EditorTemplate的名字,因为这混乱了“MVC魔术师”(see this question这也解释了它自动遍历列表模板名称,不模板名称):

@Html.BeginForm(){ 
    @Html.EditorFor(m=> m.FirstCheckboxList) 
    @Html.EditorFor(m=> m.SecondCheckboxList) 
} 
+0

_感谢您的回复_ 1. \t制作列表没有任何区别。 2. \t如果我采取For循环如何,那么我会得到错误的权利? 传入字典的模型项目类型为'System.Collections.Generic.List'1 [CheckboxItem]',但是此字典需要一个类型为CheckboxItem的模型项目 – kum123 2013-04-29 15:55:04

+0

对不起'列表'建议无效。至于第二点,请尝试通过删除templatename来更改'EditorFor'调用: '@ Html.BeginForm(){ @ Html.EditorFor(m => m。FirstCheckboxList) @ Html.EditorFor(m => m.SecondCheckboxList) }' – theyetiman 2013-04-29 16:03:42

+0

您能告诉我生成的HTML吗?将'id'提供给复选框可能有问题,而不是让模型绑定器自己设置 – theyetiman 2013-04-29 16:07:36