asp.net-mvc MVC & RadioButtonList

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/345396/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-07 23:12:00  来源:igfitidea点击:

MVC & RadioButtonList

asp.net-mvchtml.radiobuttonlist

提问by devforall

I tried doing this but this only display the radiobutton without text beside it..

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

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

回答by Zack Peterson

Elijah Manorwrote about the same trouble in ASP.NET MVC 1.0:

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

ASP.NET MVC Html.RadioButtonList Blues

ASP.NET MVC Html.RadioButtonList 蓝调

He decided to loop through his DataSource and create individual Html.RadioButton and Label combinations.

他决定遍历他的 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>
<% } %>

回答by ccook

It used to be in the previews but it was removed.

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

If you can't find it in the futures try something like this

如果您在期货中找不到它,请尝试这样的操作

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

回答by brady gaster

If it were me I would just use a series of static HTML elements. I know some consider doing such a throwback to the ASP days, but it simplifies things IMO and ends up making for a more dependable and expectable [so I made up a word] GUI.

如果是我,我只会使用一系列静态 HTML 元素。我知道有些人考虑过回到 ASP 时代,但它简化了 IMO 并最终打造了一个更可靠和更可期待的 [所以我编了一个词] GUI。

回答by charith

@{
    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/>
}

回答by A.Dara

See the nice helper By Daniel Gidman, 14 Jun 2012, here. He has created a nice and perfect helper for RadioButtonList in MVC.

请在此处查看 Daniel Gidman 的好帮手,2012 年 6 月 14 日 。他为 MVC 中的 RadioButtonList 创建了一个很好的完美助手。

回答by Matthew

Check out the MVC Futures DLL available in the MVC sourceon codeplex. In it there is an HtmlHelper extension to render RadioButton lists. It can automatically render a SelectList in the ViewData or you can pass it explicitly. Several overloads are available for different needs.

查看codeplex 上的MVC 源代码中提供的 MVC Futures DLL 。其中有一个 HtmlHelper 扩展来呈现 RadioButton 列表。它可以在 ViewData 中自动呈现 SelectList 或者您可以显式传递它。多种重载可用于满足不同的需求。