asp.net-mvc – MVC和RadioButtonList

我尝试这样做,但这只显示无旁边的单选按钮。
<% foreach (string s in Html.RadioButtonList("rbl")) {%>
    <% =s %>
<% } %>

解决方法

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>
<% } %>

相关文章

引言 本文从Linux小白的视角, 在CentOS 7.x服务器上搭建一个...
引言: 多线程编程/异步编程非常复杂,有很多概念和工具需要...
一. 宏观概念 ASP.NET Core Middleware是在应用程序处理管道...
背景 在.Net和C#中运行异步代码相当简单,因为我们有时候需要...
HTTP基本认证 在HTTP中,HTTP基本认证(Basic Authenticatio...
1.Linq 执行多列排序 OrderBy的意义是按照指定顺序排序,连续...