asp.net-mvc html.radiobuttonlist

asp.net mvc - MVC y RadioButtonList



asp.net-mvc html.radiobuttonlist (6)

Intenté hacer esto, pero esto solo muestra el botón de radio sin texto al lado.

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


Si fuera yo, solo usaría una serie de elementos HTML estáticos. Sé que algunos consideran hacer semejante retroceso a los días de la ASP, pero simplifica las cosas de la OMI y termina haciendo una GUI más confiable y esperable [por lo que hice una palabra].


Consulte la DLL MVC Futures disponible en la fuente de MVC en codeplex. En él hay una extensión HtmlHelper para mostrar listas de RadioButton. Puede mostrar automáticamente una lista de selección en ViewData o puede pasarla de forma explícita. Varias sobrecargas están disponibles para diferentes necesidades.


Solía ​​estar en las vistas previas, pero fue eliminado.

Si no puede encontrarlo en el futuro, intente algo como esto

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


Ver el buen ayudante Por Daniel Gidman, 14 de junio de 2012, aquí . Ha creado un ayudante agradable y perfecto para RadioButtonList en MVC.


Elijah Manor escribió sobre el mismo problema en ASP.NET MVC 1.0:

ASP.NET MVC Html.RadioButtonList Blues

Decidió recorrer su DataSource y crear combinaciones individuales Html.RadioButton y 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> <% } %>


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