0

だから、RadioButtonFor の選択された値を返す方法を理解しようとしています。シンプルなはずですが、何かが欠けています。

私が持っているビューで:

@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 />")
}

コントローラーには、次のものがあります。

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

AccountHelper には、次のようなものがあります。

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"));

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

RadioButtonFor を次のように変更しようとしました

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

ただし、それでも何も選択されません (ページまたは投稿に移動したとき)。

デバッグ モードで実行中に AccountHelper を確認したところ、「なし」がチェックされていることが示されていますが、それは反映されていません。

どんな助けでも大歓迎です!

4

2 に答える 2

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; }
   ...
}
于 2016-09-26T20:21:36.587 に答える
0

理解した。リストをリストに変更しました。

次に、私が持っているビューで:

@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 を実行する必要がありました。

于 2016-09-26T21:05:54.150 に答える