2008-12-05 9 views
19

He intentado hacer esto, pero esto sólo muestran el botón de radio sin texto junto a él ..MVC y RadioButtonList

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

usa el ayudante de radiolista desde aquí: http://awesome.codeplex.com – Omu

Respuesta

7

Si fuera yo me acaba de utilizar una serie de elementos HTML estáticas. 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].

-1

Consulte la DLL MVC Futures disponible en el MVC source 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.

+1

Pero no representa la etiqueta, como lo dice la pregunta. – usr

8

Solía ​​estar en las vistas previas, pero se eliminó.

Si no se puede encontrar en los futuros de intentar 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 /> 
<% } %> 
12

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

ASP.NET MVC Html.RadioButtonList Blues

Decidió recorre su DataSource y crea 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> 
<% } %> 
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/> 
} 
0

Ver el buen ayudante Por Daniel Gidman 14 Jun 2012, here. Ha creado un ayudante agradable y perfecto para RadioButtonList en MVC.