所以,我试图弄清楚如何返回 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,它显示“无”被选中,但它没有反映这一点。
任何帮助将不胜感激!