2016-09-26 11 views
0

したがって、RadioButtonForの選択値を返す方法を理解しようとしています。シンプルでなければならないが、私は何かを欠いている。ビューで RadioButton選択値を返さない

私が持っている:

コントローラで
@for (int i = 0; i < Model.ProfessionalRelations.Count; i++) 
{ 
    @Html.RadioButtonFor(m => m.SelectedProfessionalRelations, Model.ProfessionalRelations, new { id = "professionalRelations_" + i}) 
    @Html.HiddenFor(m => m.ProfessionalRelations[i].Text) 
    @Html.Raw("<span style=\"padding-left: 5px; \">"); 
    @Html.DisplayFor(m => m.ProfessionalRelations[i].Text) 
    @Html.Raw("</span><br />") 
} 

、私は以下があります。AccountHelperで

rvm.ProfessionalRelations = new List<RadioButton>(); 
rvm.ProfessionalRelations.AddRange(AccountHelper.professionalRelations(rvm.SelectedProfessionalRelations)); 

、ここで私が持っているものです。

public static List<RadioButton> professionalRelations(string selectedText) 
{ 
    var pr = new List<RadioButton>() 
    { 
     new RadioButton {Text = "None", ToolTip = "None", Checked = selectedText == "None"}, 
     new RadioButton {Text = "Orthotic/Prosthetic Practitioner", ToolTip = "Orthotic/Prosthetic Practitioner", Checked = selectedText == "Orthotic/Prosthetic Practitioner"}, 
     new RadioButton {Text = "Orthotic/Prosthetic Resident", ToolTip = "Orthotic/Prosthetic Resident", Checked = selectedText == "Orthotic/Prosthetic Resident"}, 
     new RadioButton {Text = "Orthotic/Prosthetic Student", ToolTip = "Orthotic/Prosthetic Student", Checked = selectedText == "Orthotic/Prosthetic Student"}, 
     new RadioButton {Text = "O&P Technician (including Students)", ToolTip = "O&P Technician (including Students)", Checked = selectedText == "O&P Technician (including Students)"}, 
     new RadioButton {Text = "Non-Clinical Employee of an O&P practice", ToolTip = "Non-Clinical Employee of an O&P practice", Checked = selectedText == "Non-Clinical Employee of an O&P practice"}, 
     new RadioButton {Text = "Employee of an O&P Manfacturer/Distributor or other related company", ToolTip = "Employee of an O&P Manfacturer/Distributor or other related company", Checked = selectedText == "Employee of an O&P Manfacturer/Distributor or other related company"}, 
     new RadioButton {Text = "HealthCare Professional (non O&P)", ToolTip = "HealthCare Professional (non O&P)", Checked = selectedText == "HealthCare Professional (non O&P)"}, 
     new RadioButton {Text = "Other", ToolTip = "Other", Checked = selectedText == "Other"} 
    }; 
    return pr; 
} 

とき私は投稿:

rvm.ProfessionalRelations = new List<RadioButton>(); 
rvm.ProfessionalRelations.AddRange(AccountHelper.professionalRelations(rvm.SelectedProfessionalRelations)); 

しかし、rvm.SelectedProfessionalRelationsはあなたが行くとき「System.Collections.Generic.List`1は[System.Web.UI.WebControls.RadioButton]」

私がいることを追加する必要があり として戻っ何らかの理由私は以下を持っています:

rvm.ProfessionalRelations = new List<RadioButton>(); 
rvm.ProfessionalRelations.AddRange(AccountHelper.professionalRelations("None")); 

何も選択されていません。

私は

@Html.RadioButtonFor(m => m.SelectedProfessionalRelations, Model.ProfessionalRelations, new { id = "professionalRelations_" + i, @checked = Model.ProfessionalRelations[i].Checked}) 

するRadioButtonForを変更しようとしたが、それはまだ何も選択しない(どちらかのページまたはポストに行くとき)。

デバッグモードで実行中、私はAccountHelperをチェックし、 "なし"がチェックされているがそれはそれを反映していないことを示しています。

ご協力いただければ幸いです!

+0

次の '@ Html.RadioButtonFor'の簡単なGoogle検索を実行しましたか? – MethodMan

+0

' RadionButtonFor'の2番目のパラメータは、この特定のボタンの値にする必要があります。おそらく、それは 'Model.ProfessionalRelations [i] .Text'なのか、それと似たものなのでしょうか。あなたが隠れたフィールドで何をやっているのかわからない...ちょっと変わったようです。 –

+0

これをModel.ProfessionalRelations [i] .Textに変更しましたが、まだ運がありません。 – ajtatum

答えて

0

RadioButtonForの3番目のパラメータは、我々は通常、文字列引数を渡すオブジェクトです。ラジオボタンは、単一の選択された値を返すため

@for (int i = 0; i < Model.ProfessionalRelations.Count; i++) 
{ 
    @Html.RadioButtonFor(m => m.SelectedProfessionalRelation, 
     Model.ProfessionalRelations[i].Text) 
    Model.ProfessionalRelations[i].Text 
} 

モデルクラスは、特異(複数ではない)としてSelectedProfessionalRelationプロパティと次のようであるべきです。

public class YourModel 
{ 
    ... 
    public string SelectedProfessionalRelation { get; set; } 
    ... 
} 
0

List to Listを変更しました。

は、その後、私は持っているビューで:値は時々スラッシュとはwhatnowを含んでいて

@foreach (var pr in Model.ProfessionalRelations) 
{ 
    @Html.RadioButtonFor(m => m.SelectedProfessionalRelations, pr, new { id = pr.Replace("/",string.Empty).Replace(" ",string.Empty) }) 
    @Html.Raw("<span style=\"padding-left: 5px;\">") 
    @Html.LabelFor(m=>m.ProfessionalRelations, pr, new {@for = pr.Replace("/", string.Empty).Replace(" ", string.Empty) }) 
    @Html.Raw("</span>") 

    if (pr == "Other") 
    { 
     <span style="padding-left: 10px;">@Html.TextBox("Other")</span> 
    } 
    @Html.Raw("<br />") 
} 

私はstring.Replaceをしなければなりませんでした。

+0

'それを考え出した。 List to Listを変更しました。それは完全に理にかなっています。 ¯/ _(ツ)_ /¯ – Win

関連する問題