How can i Set RadioButtonFor() as Checked By Default
<%=Html.RadioButtonFor(m => m.Gender,\"Male\")%>
there is way out for (Html.Radio
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(this HtmlHelper htmlHelper, Expression> expression, object value)
{
return CheckedRadioButtonFor(htmlHelper, expression, value, null);
}
public static MvcHtmlString CheckedRadioButtonFor(this HtmlHelper htmlHelper, Expression> 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: