如何在 MVC 表单中将 CheckBoxFor 与其他表单控件一起使用
How to use CheckBoxFor in MVC forms with other form controls
基本上,我有一个带有文本框、单选按钮和复选框控件的表单。现在我在提交页面时遇到复选框控件的问题
我有这样的模型
public class PersonDetails
{
public int personID { get; set; }
public string PersonName { get; set; }
public string Gender { get; set; }
public List<Education> Education { get; set; }
public string EmailID { get; set; }
public string Address { get; set; }
}
public class Education
{
public string Qualification { get; set; }
public bool Checked { get; set; }
public List<Education> GetQualification()
{
return new List<Education>{
new Education {Qualification="SSC",Checked=false},
new Education {Qualification="HSC",Checked=false},
new Education {Qualification="Graduation",Checked=false},
new Education {Qualification="PostGraduation",Checked=false}
};
}
}
我有这样的看法
@using (Html.BeginForm("GetDetails", "User", FormMethod.Post, new { id = "person-form" }))
{
<div class="col-xs-12">
<label>Person Name</label>
@Html.TextBoxFor(x => x.PersonName)
</div>
<div class="col-xs-12">
<label>Gender</label>
@Html.RadioButtonFor(x => x.Gender, "Male")
@Html.RadioButtonFor(x => x.Gender, "Female")
</div>
<div class="col-xs-12">
<label>Education</label>
@{
Html.RenderPartial("Qualification", new LearnAuthentication.Controllers.Education().GetQualification());
}
</div>
<div class="col-xs-12">
<input type="submit" value="Submit" />
</div>
}
和这样的局部视图
@model List<LearnAuthentication.Controllers.Education>
<br />
@for (int i = 0; i < Model.Count(); i++)
{
@Html.HiddenFor(x => Model[i].Qualification)
@Html.CheckBoxFor(x => Model[i].Checked)
@Html.DisplayFor(x => Model[i].Qualification)
<br />
}
我的操作方法是这样的
[HttpPost]
public ActionResult GetDetails(PersonDetails personDetails)
{
return View();
}
现在,当我 运行 我的应用程序时,我倾向于获取所有信息,但是当我提交页面时,我得到的 属性 具有空值
public List Education { get; set; }
你们中的任何人都可以帮助我解决我做错了什么,或者您可以指导我如何实现这一目标的正确道路。
您使用部分生成 Education
的控件正在生成诸如
之类的输入
<input type="hidden" name="[0].Qualification" ... />
<input type="hidden" name="[1].Qualification" ... />
但为了绑定,它们需要具有与您的模型相匹配的名称属性
<input type="hidden" name="Education[0].Qualification" ... />
<input type="hidden" name="Education[1].Qualification" ... />
将你的部分重命名为 Education.cshtml
(以匹配 class 的名称)并将其移动到你的 /Views/Shared/EditorTemplates
文件夹(或者 /Views/yourControllerName/EditorTemplates
如果你想要一个特定的仅适用于该控制器的模板)
然后把偏音改成
@model LearnAuthentication.Controllers.Education
@Html.HiddenFor(m => m.Qualification)
@Html.LabelFor(m => m.Checked)
@Html.CheckBoxFor(m => m.Checked)
@Html.DisplayFor(m => m.Qualification)
并在主视图中替换
<label>Education</label>
@{ Html.RenderPartial("Qualification", new LearnAuthentication.Controllers.Education().GetQualification()); }
和
<span>Education</span> // its not a label
@Html.EditorFor(m => m.Education)
这将为您的 collection
中的每个项目正确生成正确的 html
旁注:其他可行的替代方法是将 POST 方法签名更改为
[HttpPost]
public ActionResult GetDetails(PersonDetails personDetails List<Education> educationDetails)
或将 HtmlFieldPrefix
传递给部分,如 getting the values from a nested complex object that is passed to a partial view
中所述
基本上,我有一个带有文本框、单选按钮和复选框控件的表单。现在我在提交页面时遇到复选框控件的问题 我有这样的模型
public class PersonDetails
{
public int personID { get; set; }
public string PersonName { get; set; }
public string Gender { get; set; }
public List<Education> Education { get; set; }
public string EmailID { get; set; }
public string Address { get; set; }
}
public class Education
{
public string Qualification { get; set; }
public bool Checked { get; set; }
public List<Education> GetQualification()
{
return new List<Education>{
new Education {Qualification="SSC",Checked=false},
new Education {Qualification="HSC",Checked=false},
new Education {Qualification="Graduation",Checked=false},
new Education {Qualification="PostGraduation",Checked=false}
};
}
}
我有这样的看法
@using (Html.BeginForm("GetDetails", "User", FormMethod.Post, new { id = "person-form" }))
{
<div class="col-xs-12">
<label>Person Name</label>
@Html.TextBoxFor(x => x.PersonName)
</div>
<div class="col-xs-12">
<label>Gender</label>
@Html.RadioButtonFor(x => x.Gender, "Male")
@Html.RadioButtonFor(x => x.Gender, "Female")
</div>
<div class="col-xs-12">
<label>Education</label>
@{
Html.RenderPartial("Qualification", new LearnAuthentication.Controllers.Education().GetQualification());
}
</div>
<div class="col-xs-12">
<input type="submit" value="Submit" />
</div>
}
和这样的局部视图
@model List<LearnAuthentication.Controllers.Education>
<br />
@for (int i = 0; i < Model.Count(); i++)
{
@Html.HiddenFor(x => Model[i].Qualification)
@Html.CheckBoxFor(x => Model[i].Checked)
@Html.DisplayFor(x => Model[i].Qualification)
<br />
}
我的操作方法是这样的
[HttpPost]
public ActionResult GetDetails(PersonDetails personDetails)
{
return View();
}
现在,当我 运行 我的应用程序时,我倾向于获取所有信息,但是当我提交页面时,我得到的 属性 具有空值
public List Education { get; set; }
你们中的任何人都可以帮助我解决我做错了什么,或者您可以指导我如何实现这一目标的正确道路。
您使用部分生成 Education
的控件正在生成诸如
<input type="hidden" name="[0].Qualification" ... />
<input type="hidden" name="[1].Qualification" ... />
但为了绑定,它们需要具有与您的模型相匹配的名称属性
<input type="hidden" name="Education[0].Qualification" ... />
<input type="hidden" name="Education[1].Qualification" ... />
将你的部分重命名为 Education.cshtml
(以匹配 class 的名称)并将其移动到你的 /Views/Shared/EditorTemplates
文件夹(或者 /Views/yourControllerName/EditorTemplates
如果你想要一个特定的仅适用于该控制器的模板)
然后把偏音改成
@model LearnAuthentication.Controllers.Education
@Html.HiddenFor(m => m.Qualification)
@Html.LabelFor(m => m.Checked)
@Html.CheckBoxFor(m => m.Checked)
@Html.DisplayFor(m => m.Qualification)
并在主视图中替换
<label>Education</label>
@{ Html.RenderPartial("Qualification", new LearnAuthentication.Controllers.Education().GetQualification()); }
和
<span>Education</span> // its not a label
@Html.EditorFor(m => m.Education)
这将为您的 collection
中的每个项目正确生成正确的 html旁注:其他可行的替代方法是将 POST 方法签名更改为
[HttpPost]
public ActionResult GetDetails(PersonDetails personDetails List<Education> educationDetails)
或将 HtmlFieldPrefix
传递给部分,如 getting the values from a nested complex object that is passed to a partial view