Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC & RadioButtonList

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

<% foreach (string s in Html.RadioButtonList("rbl")) {%>
    <% =s %>
<% } %> 
like image 496
devforall Avatar asked Dec 05 '08 22:12

devforall


4 Answers

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>
<% } %>
like image 106
Zack Peterson Avatar answered Nov 11 '22 05:11

Zack Peterson


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 />
<% } %>
like image 35
ccook Avatar answered Nov 11 '22 03:11

ccook


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.

like image 7
brady gaster Avatar answered Nov 11 '22 03:11

brady gaster


@{
    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/>
}
like image 6
charith Avatar answered Nov 11 '22 04:11

charith