views:

3706

answers:

8

hi, How can i Set RadioButtonFor() as Checked By Default

<%=Html.RadioButtonFor(m => m.Gender,"Male")%>

there is way out for (Html.RadioButton) but not for (Html.RadioButtonFor)

any Ideas?

+3  A: 

I assume you should have a group of radio buttons. something could be like

<%=Html.RadioButtonFor(m => m.Gender,"Male")%>
<%=Html.RadioButtonFor(m => m.Gender,"Female")%>
<%=Html.RadioButtonFor(m => m.Gender,"Unknown")%>

You may give the default value for m.Gender = "Unknown" (or something) from your controller.

Allen Wang
But i want only two radiobutton with one of them checked bydefault using Html.RadiobuttonFor()
+3  A: 

This question on StackOverflow deals with RadioButtonListFor and the answer addresses your question too. You can set the selected property in the RadioButtonListViewModel.

Mac
A: 

If you're using jquery, you can call this right before your radio buttons.

$('input:radio:first').attr('checked', true);

^ This will check the first radio box, but you can look at more jquery to cycle through to the one you want selected.

rrp6119
A: 

It's not too pretty, but if you have to implement only very few radio buttons for the entire site, something like this might also be an option:

<%=Html.RadioButtonFor(m => m.Gender,"Male",new { @checked = Model.Gender=="Male"?"checked":"" })%>

Adrian Grigore
+4  A: 

Use <%= Html.RadioButtonFor(m => m.Gender, "Male", new { Checked = "checked" })%> the simple way...

Gabriel Militello
+1  A: 

This Helper evaluates the expression and if equals to the value it checks the radio button, and has the same parameters than RadioButtonFor (for this reason the name is diferent):

public static MvcHtmlString CheckedRadioButtonFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, object value)
{
    return CheckedRadioButtonFor(htmlHelper, expression, value, null);
}

public static MvcHtmlString CheckedRadioButtonFor<TModel, TPropery>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, object value, object htmlAttributes)
{
    var func = expression.Compile();
    var attributes = new RouteValueDictionary(htmlAttributes);
    if ((object)func(htmlHelper.ViewData.Model) == value) {
        attributes["checked"] = "checked";
    }
    return htmlHelper.RadioButtonFor(expression, value, attributes);
}

Usage:

<%= Html.CheckedRadioButtonFor(m => m.Gender, "Male", new { id = "gender-male" })%>

Result:

<!-- For Model.Gender = "Male" -->
<input checked="checked" id="gender-male" name="Gender" type="radio" value="Male">
<!-- For Model.Gender = "Female" -->
<input id="gender-male" name="Gender" type="radio" value="Male">
Alex LE
A: 

You can also add labels that are tied to your radio buttons with the same ID, which then allows the user to click the radio button or label to select that item. I'm using constants here for "Male", "Female" and "Unknown", but obviously these could be strings in your model.

<%: Html.RadioButtonFor(m => m.Gender, "Male", 
    new Dictionary<string, object> { { "checked", "checked" }, { "id", "Male" } }) %>
<%: Html.Label("Male") %>

<%: Html.RadioButtonFor(m => m.Gender, "Female", 
    new Dictionary<string, object> { { "id", "Female" } }) %>
<%: Html.Label("Female")%>

<%: Html.RadioButtonFor(m => m.Gender, "Unknown",
    new Dictionary<string, object> { { "id", "Unknown" } }) %>
<%: Html.Label("Unknown")%>
nekno
A: 
<%: Html.RadioButtonFor(m => m.Gender, "Male", new { @checked = true } )%>

or

@checked = checked

if you like

Markus Sjöholm