Flexabust Bergson Flexabust Bergson -3 years ago 165
C# Question

Can't get default check on RadioButtonFor

I have a razor

that I am trying to check by default but nothing works. Here is my Code:

@Html.RadioButtonFor(m=>m.selected, new {@checked=true});

is a boolean. And the Html Generated:

<input name="selected" id="selected" type="radio"
value="{ Name = groupRadio, checked = True }">

I've also tried

@Html.RadioButtonFor(m=>m.selected, 0, new {@checked=true});

which didn't work either. Any ideas?


Answer Source

If selected is bool, then your radio buttons need to be

    @Html.RadioButtonFor(m=>m.selected, true, new { id = "" });
@Html.RadioButtonFor(m=>m.selected, false, new { id = "" });

Note the 2nd parameter generates the value attribute to bind to your bool property.

Then if the value of selected is true, the first will be selected, otherwise the 2nd will be

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download