2008-12-05 61 views
19

我想这样做,但这个只是没有它旁边的文本显示单选按钮..MVC和单选按钮列表

<% foreach (string s in Html.RadioButtonList("rbl")) {%> 
    <% =s %> 
<% } %> 
+0

使用这里的radiolist助手:http://awesome.codeplex.com – Omu 2011-05-20 17:26:33

回答

7

如果是我,我只想用一系列静态HTML元素。我知道有些人认为做ASP这样的倒退,但它简化了国际海事组织,并最终形成了一个更可靠和可预期的[所以我编造了一个词]图形用户界面。

-1

查看codeplex上的MVC source中提供的MVC Futures DLL。其中有一个HtmlHelper扩展来渲染RadioButton列表。它可以自动在ViewData中渲染一个SelectList,或者你可以显式地传递它。有几种过载可用于不同的需求。

+1

但它不会呈现标签,因为问题所述。 – usr 2010-01-13 14:20:08

8

它曾经在预览中,但它被删除。

如果你不能在期货发现它尝试这样的事情

<% foreach (Model model in Models)) 
    { 
%><%= String.Format("<input type=\"radio\" value=\"{0}\" name=\"{1}\" id=\"{2}\"><label for=\"{2}\">{3}</label>", 
     model.ID, "fieldName", model.modelID, model.Name) %><br /> 
<% } %> 
12

Elijah Manor写在ASP.NET MVC 1.0同样的烦恼:

ASP.NET MVC Html.RadioButtonList Blues

他决定遍历他的DataSource并创建单独的Html.RadioButton和Label组合。

<!-- After using and looking at the code for the Html.RadioButtonList in the ASP.NET MVC 1.0 RTM codebase, I'm not sure how it is supposed to be useful. It only outputs the actual input radio button and doesn't render any corresponding labels. To get around this I ended up writing a foreach creating individual Html.RadioButton and labels --> 
<% 
var radioButtonList = new SelectList(new List<ListItem> { 
    new ListItem { Text = "Current", Value="false", Selected=true }, 
    new ListItem { Text = "Other", Value="true"}}, "Value", "Text", "false"); 
var htmlAttributes = new Dictionary<string, object> { 
    { "class", "radioButtonList" }, 
    { "onclick", "if(eval(this.value)) { $('#tblDate').show('slow'); } else { $('#tblDate').hide('slow'); }" } 
}; 
foreach (var radiobutton in radioButtonList) { %> 
    <%=Html.RadioButton("rblDate", radiobutton.Value, radiobutton.Selected, htmlAttributes)%> 
    <label><%=radiobutton.Text%></label> 
<% } %> 
6
@{ 
    var radioButtonList = new SelectList(new List<ListItem> { 
    new ListItem { Text = "1", Value="true", Selected=true }, 
    new ListItem { Text = "2", Value="false"}, 
    new ListItem { Text = "3", Value="false"}, 
    new ListItem { Text = "4", Value="false"}, 

    }, "Value", "Text", "false"); 

    var htmlAttributes = new Dictionary<string, object> { 
    { "class", "radioButtonList" }, 
    { "onclick", "if(eval(this.value)) { $('#tblDate').show('slow'); } else { $('#tblDate').hide('slow'); }" } 
};  
      } 

@foreach (var radiobutton in radioButtonList) { 

    @Html.RadioButtonFor(m => m.ContactDepartment, @radiobutton.Text) @radiobutton.Text 

    <br/> 
} 
0

看到漂亮的助手丹尼尔Gidman,2012年6月14日,here。 他为MVC中的RadioButtonList创建了一个漂亮完美的助手。