所以,我想弄清楚如何返回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”被选中,但没有反映出来。
任何帮助将不胜感激!
你在下面的'@ Html.RadioButtonFor'上执行了一个简单的谷歌搜索。 – MethodMan
'RadionButtonFor'的第二个参数应该是这个特定按钮的值。所以可能你应该是'Model.ProfessionalRelations [i] .Text'或类似的东西。不知道你在隐藏的领域做什么,但...似乎有点奇怪。 –
将其更改为Model.ProfessionalRelations [i] .Text,但仍然没有运气。 – ajtatum