RadioButton不返回选定值

时间:2016-09-26 16:30:02

标签: c# asp.net-mvc asp.net-mvc-5 radiobuttonfor

所以,我试图弄清楚如何返回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返回为 &#34; System.Collections.Generic.List`1 [System.Web.UI.WebControls.RadioButton]&#34;

我应该补充一点,当你去页面时,我有以下内容:

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并显示&#34;无&#34;已经过检查,但它没有反映出来。

非常感谢任何帮助!

2 个答案:

答案 0 :(得分:0)

RadioButtonFor 的第三个参数是我们通常传递 string 参数的对象。

@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; }
   ...
}

答案 1 :(得分: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。