首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MVC & RadioButtonList

MVC & RadioButtonList
EN

Stack Overflow用户
提问于 2008-12-05 22:29:45
回答 6查看 29.6K关注 0票数 21

我尝试过这样做,但这只显示了单选按钮,而旁边没有文本。

代码语言:javascript
复制
<% foreach (string s in Html.RadioButtonList("rbl")) {%>
    <% =s %>
<% } %> 
EN

回答 6

Stack Overflow用户

发布于 2009-04-02 20:22:48

Elijah Manor在ASP.NET MVC1.0中写到了同样的问题:

ASP.NET MVC Html.RadioButtonList Blues

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

代码语言:javascript
复制
<!-- 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>
<% } %>
票数 13
EN

Stack Overflow用户

发布于 2009-01-04 02:16:36

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

如果你在未来找不到它,试试下面这样的东西

代码语言:javascript
复制
<% 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 />
<% } %>
票数 9
EN

Stack Overflow用户

发布于 2008-12-09 18:21:42

如果是我,我只会使用一系列静态HTML元素。我知道有些人会考虑回到ASP时代,但它简化了我的工作,最终使它变得更加可靠和可预期,所以我造了一个单词GUI。

票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/345396

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档