基本上,我有一個帶有文本框,單選按鈕和複選框控件的窗體。現在我面臨的問題,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值
公開名單教育{獲得;組; }
可以你們中的任何一個人幫我解決我做錯了什麼,或者你能指導我如何實現這一目標的正確途徑。
@StephenMuecke這聽起來好笑向你解釋,但我真的不知道如何接受答案,作爲讚賞我總是投票支持我的答案:) 現在我正在通過博客去學習如何投票答案..謝謝你,請幫助我的問題,如果你知道答案 –
@StephenMuecke嘿..謝謝你的反饋,我終於知道如何接受答案...再次感謝你,請讓我知道如果你知道我的問題的答案:) –