如何在其他窗体控件中使用MVC窗体中的CheckBoxFor
基本上,我有一个带有文本框,单选按钮和复选框控件的窗体。现在我面临的问题,CheckBox控件,当我提出我的网页 我有这样如何在其他窗体控件中使用MVC窗体中的CheckBoxFor
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();
}
现在,当我跑我的应用我往往会得到所有的信息,但是当我提交页面我得到这个属性具有NULL值
公开名单教育{获得;组; }
可以你们中的任何一个人帮我解决我做错了什么,或者你能指导我如何实现这一目标的正确途径。
您使用的部分,以产生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
(以匹配课程名称)并将其移动到您的/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)
将正确生成您的收藏中每个项目的正确的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
谢谢你的回答我欣赏它:) –
@StephenMuecke这听起来好笑向你解释,但我真的不知道如何接受答案,作为赞赏我总是投票支持我的答案:) 现在我正在通过博客去学习如何投票答案..谢谢你,请帮助我的问题,如果你知道答案 –
@StephenMuecke嘿..谢谢你的反馈,我终于知道如何接受答案...再次感谢你,请让我知道如果你知道我的问题的答案:) –