Flexabust Bergson
Flexabust Bergson

Reputation: 762

Can't get default check on RadioButtonFor

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

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

Where selected 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?

Thanks

Upvotes: 0

Views: 914

Answers (1)

user3559349
user3559349

Reputation:

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

<label>
    @Html.RadioButtonFor(m=>m.selected, true, new { id = "" });
    <span>Yes</span>
</label>
<label>
@Html.RadioButtonFor(m=>m.selected, false, new { id = "" });
    <span>No</span>
</label>

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

Upvotes: 3

Related Questions