2011-12-13 81 views
2

我有一個yes/no單選按鈕,需要訪問另一個aspx表單上的值。我不知道我是否做對了?訪問IEnumerable mvc2單選按鈕的值

主視圖模型

[UIHint("YesNoEditorTemplate")] 
[DisplayName("Are you registered as blind (severely sight impaired)?")] 
public IEnumerable<RadioButtonViewModel> RegBlind { get; set; } 

單選按鈕視圖模型

public class RadioButtonViewModel 
{ 
    public string ID { get; set; } 
    public string Name { get; set; } 
    public int Value { get; set; } 
    public string Text { get; set; } 
} 

控制器

List<RadioButtonViewModel> regBlindList = new List<RadioButtonViewModel>(); 
regBlindList.Add(CreateRadioButton("RegBlind", 1, "Yes")); 
regBlindList.Add(CreateRadioButton("RegBlind", 0, "No")); 
badgeViewModel.RegBlind = regBlindList; 

編輯模板

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<Tameside.Internal.ViewModels.BlueBadge.RadioButtonViewModel>>"%><% 
foreach (var model in Model) 
{%> 
    <tr>  
     <td><%=String.Format("<input type=\"radio\" id=\"{0}\" name=\"{1}\" value=\"{2}\" />", model.ID, model.Name,model.Value)%></td> 
     <td><%=String.Format("<label for=\"{0}\">{1}</label>", model.ID, model.Text)%></td> 
    </tr><% 
}%> 

ASPX頁面

<%=Html.EditorFor(x => x.RegBlind)%> 

這裏是我正在訪問信息

if (Model.RegBlind.First().Value == 0) 

這是正確的嗎?

在此先感謝您的幫助。

克萊爾

回答

2

我認爲這個問題是,枚舉絕不會存儲選定值,這只是流失到一定程度。

所以你需要兩個部分,一個存儲值和另一個包含你想顯示的單選按鈕。

[DisplayName("Are you registered as blind (severely sight impaired)?")] 
public int RegBlind { get; set; } 

public IEnumerable<RadioButtonViewModel> RegBlindOptions { get; set; } 

一旦你得到這個排序,你應該能夠使用您的EditorTemplate作爲一個局部視圖(不過,這很可能意味着移動它的EditorTemplates目錄,進入相應的視圖文件夾或共享)。

<% Html.RenderPartial("YesNoEditorTemplate", Model.RegBlindOptions)%> 

這應該工作或接近。如果沒有,看看生成的HTML,並檢查一切正常,然後在郵件頭中發送回來的內容有一個問題:)