2016-09-26 55 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