2016-08-21 3 views
1

基本的には、テキストボックス、ラジオボタン、およびチェックボックスコントロールを持つフォームがあります。今私は私のページ を提出したときに、私はこの他のフォームコントロールとの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

で説明したように、部分的にHtmlFieldPrefixを渡すことであろう、それはあなたに面白い聞こえるかもしれ@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) 

に部分的に変更し、メインビューに置き換える正しくコレクション内の各項目について、正しいHTMLを生成します

<label>Education</label> 
@{ Html.RenderPartial("Qualification", new LearnAuthentication.Controllers.Education().GetQualification()); } 

<span>Education</span> // its not a label 
@Html.EditorFor(m => m.Education) 

サイドノート:仕事と他の代替が

[HttpPost] 
public ActionResult GetDetails(PersonDetails personDetails List<Education> educationDetails) 

にPOSTメソッドのシグネチャを変更したり、getting the values from a nested complex object that is passed to a partial view

+0

私はそれを感謝あなたの答えをありがとう:) –

関連する問題