Friday, 9 August 2013

Proper way to create a radiobuttonlist without having any of the radiobuttons checked by default

Proper way to create a radiobuttonlist without having any of the
radiobuttons checked by default

The nullable identifier makes the radiobuttonlist not check any of the
radiobuttons by default on first page load. Is this the proper way to
handle this scenario? Or what is best practice?
Model:
[Required]
public SomeEnum? Choices { get; set; }
public enum SomeEnum
{
OptionOne,
OptionTwo
}
View:
<div>
@Html.ValidationMessageFor(x => x.Choices)
@Html.RadioButtonFor(x => x.Choices, SomeEnum.OptionOne)
@Html.RadioButtonFor(x => x.Choices, SomeEnum.OptionTwo)
</div>
Rendered HTML:
<div>
<input name="Choices" id="Choices" type="radio" data-val-required="The
Choices field is required." data-val="true" value="OptionOne"></input>
<input name="Choices" id="Choices" type="radio"
value="OptionTwo"></input>
</div>

No comments:

Post a Comment