所以,我想弄清楚如何返回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