【问题标题】:@Html.Editor Model binding Issue@Html.Editor 模型绑定问题
【发布时间】:2013-04-29 15:10:36
【问题描述】:

我正在尝试在视图上生成两组复选框列表。除了发布操作之外,这一切都有效。提交时, ParentViewModel 没有正确绑定 ChildViewModel 模型。第一个复选框列表 模型。第二个复选框列表 以上两者都为空。

我不确定我错过了什么。对此的任何帮助都会很棒。 提前致谢。

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" />
 }

父视图模型

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

标签: asp.net-mvc asp.net-mvc-4


【解决方案1】:

尝试将ParentViewModel 的视图模型更改为使用List&lt;CheckboxItem&gt; 而不是IEnumerable&lt;CheckboxItem&gt;:

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; }
}

模型绑定器需要像List 或Array 这样的数据结构,以便它可以正确绑定指定索引处的元素。 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) 
} 

【讨论】:

  • 感谢您的回复 1. 制作列表没有任何区别。 2.如果我如何关闭 For 循环,那么我会得到以下错误吗?传入字典的模型项的类型为“System.Collections.Generic.List`1[CheckboxItem]”,但此字典需要 CheckboxItem 类型的模型项
  • 很抱歉,List 的建议没有奏效。至于第二点,请尝试通过删除模板名称来更改EditorFor 调用:@Html.BeginForm(){ @Html.EditorFor(m=&gt; m.FirstCheckboxList) @Html.EditorFor(m=&gt; m.SecondCheckboxList) }
  • 你能告诉我生成的 HTML 吗?将id 提供给复选框而不是让模型绑定器自己设置可能存在问题
  • @kum123 - 那么当您发布时,您的对象是否仍然为空?如果它们现在已修复,请将其标记为答案。此外,这看起来不像 HTML,而是看起来您已经从 Visual Studio 调试器中的请求对象中复制并粘贴了一个值。请编辑您的问题并包含输出到 Web 浏览器的 HTML(按 CTRL-U 或右键单击页面上的 > 查看源代码)。我将编辑我的答案以解释为什么在您提供模板名称时Html.EditorFor() 不起作用...
  • 它按照你的解释工作,但我很想知道。为什么它不起作用,当你提到模板名称时。感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 2011-12-22
  • 2019-09-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多