2016-08-21 65 views
1

基本上,我有一個帶有文本框,單選按鈕和複選框控件的窗體。現在我面臨的問題,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值

公開名單教育{獲得;組; }

可以你們中的任何一個人幫我解決我做錯了什麼,或者你能指導我如何實現這一目標的正確途徑。

+0

@StephenMuecke這聽起來好笑向你解釋,但我真的不知道如何接受答案,作爲讚賞我總是投票支持我的答案:) 現在我正在通過博客去學習如何投票答案..謝謝你,請幫助我的問題,如果你知道答案 –

+0

@StephenMuecke嘿..謝謝你的反饋,我終於知道如何接受答案...再次感謝你,請讓我知道如果你知道我的問題的答案:) –

回答

1

您使用的部分,以產生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

+0

謝謝你的回答我欣賞它:) –

相關問題