2016-09-26 53 views
0

所以,我想弄清楚如何返回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返回为 “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,它显示“None”被选中,但没有反映出来。

任何帮助将不胜感激!

+0

你在下面的'@ Html.RadioButtonFor'上执行了一个简单的谷歌搜索。 – MethodMan

+0

'RadionButtonFor'的第二个参数应该是这个特定按钮的值。所以可能你应该是'Model.ProfessionalRelations [i] .Text'或类似的东西。不知道你在隐藏的领域做什么,但...似乎有点奇怪。 –

+0

将其更改为Model.ProfessionalRelations [i] .Text,但仍然没有运气。 – ajtatum

回答

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

+0

'想通了。我将列表更改为列表。“这完全有意义。 ¯/ _(ツ)_ /¯ – Win