21

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

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

6 Answers 6

13

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

ASP.NET MVC Html.RadioButtonList Blues

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

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

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

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.

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

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

-1

Check out the MVC Futures DLL available in the MVC source on 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.

1
  • 1
    But it does not render the label, as the question stated.
    – usr
    Commented Jan 13, 2010 at 14:20

Not the answer you're looking for? Browse other questions tagged or ask your own question.