简体   繁体   English

单选按钮不返回所选值

[英]RadioButtonFor not returning the Selected Value

So, I'm trying to figure out how to return the selected value of a RadioButtonFor. 因此,我试图弄清楚如何返回RadioButtonFor的选定值。 Should be simple, but I'm missing something. 应该很简单,但我缺少了一些东西。

In the view I have: 我认为:

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

In the controller, I have the following: 在控制器中,我具有以下内容:

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

In the AccountHelper, here's what I have: 在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;
}

When I post: 当我发布:

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

But, for some reason the rvm.SelectedProfessionalRelations returns back as "System.Collections.Generic.List`1[System.Web.UI.WebControls.RadioButton]" 但是,由于某种原因,rvm.SelectedProfessionalRelations返回为“ System.Collections.Generic.List`1 [System.Web.UI.WebControls.RadioButton]”

I should add that when your go the page, I have the following: 我应该补充一点,当您转到页面时,我有以下内容:

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

But nothing is selected. 但是什么也没选择。

I've tried changing the RadioButtonFor to be 我尝试将RadioButtonFor更改为

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

But that still doesn't select anything (either when you go to the page or post). 但这仍然没有选择任何内容(无论是转到页面还是发布)。

While running in debug mode, I checked the AccountHelper and it shows that "None" is checked but it doesn't reflect that. 在调试模式下运行时,我检查了AccountHelper,它显示已选中“无”,但没有反映出来。

Any help would be greatly appreciated! 任何帮助将不胜感激!

RadioButtonFor 's third parameter is an object which we normally pass string argument. RadioButtonFor的第三个参数是我们通常传递字符串参数的对象。

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

Your model class should be the following with SelectedProfessionalRelation property as singular (not plural) , because radio button only returns a single selected value. 您的模型类应为以下类,其中SelectedProfessionalRelation属性为单数 (而不是复数) ,因为单选按钮仅返回单个选定值。

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

Figured it out. 弄清楚了。 I changed List to List. 我将列表更改为列表。

Then in the View I have: 然后在视图中,我有:

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

I had to do the string.Replace as the values sometimes contained slashes and whatnow. 我不得不做字符串。替换为有时值包含斜杠和nownow。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM