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的第三个参数是我们通常传递字符串参数的对象。

@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 因为值有时包含斜杠和whatnow。

于 2016-09-26T21:05:54.150 回答