I tried doing this but this only display the radiobutton without text beside it..
<% foreach (string s in Html.RadioButtonList("rbl")) {%>
<% =s %>
<% } %>
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>
<% } %>
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 />
<% } %>
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.
@{
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/>
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With