views:

6950

answers:

4

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

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

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.

brady gaster
A: 

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.

Matthew
But it does not render the label, as the question stated.
usr
+4  A: 

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 />
<% } %>
ccook
+4  A: 

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