c# - Proper way to create a radiobuttonlist without having any of the radiobuttons checked by default -
the nullable identifier makes radiobuttonlist not check of radiobuttons default on first page load. proper way handle scenario? or 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 required." data-val="true" value="optionone"></input> <input name="choices" id="choices" type="radio" value="optiontwo"></input> </div>
yes, using best model represent radio list no default value. right purpose null
.
an advantage of approach [required]
attribute makes easy validate option has been selected.
Comments
Post a Comment