I am using radio button like this,
<div class="form-group">
@Html.LabelFor(x => x.gender)
<div class="form-check">
<label class="form-check-label">
@Html.RadioButtonFor(x => x.gender, "Male")
Male
</label>
</div>
<div class="form-check">
<label class="form-check-label">
@Html.RadioButtonFor(x => x.gender, "Female")
Female
</label>
</div>
</div>
here i have two radio button which is rendered in the page, i want the radio button to be rendered and without the default selection of any of the radio button, how can i do this
i tried like this but it is not working
@Html.RadioButtonFor(x => x.gender, "Female", new { @checked = "true" })
UPDATE
public class student
{
[DisplayName("Gender")]
[Required(ErrorMessage = "please select gender")]
public gender gender { get; set; }
}
public enum gender
{
male,
female
}
To make sure nothing is selected by default, make your property nullable:
public gender? gender { get; set; }
Its default value will be null
, and that will make sure nothing is selected, but Required
attribute won't allow null
as valid value once user posted the form.
On a separate note, you are using strings as values right now, I am not sure that is going to work with enum at all. It would be much better to do something along these lines:
@Html.RadioButtonFor(x => x.gender, gender.Female)
@Html.Label(gender.Female.ToString())
// Or you can stick to literals for labels as you do now.
// This however allows you to eventually turn this rendering code into a loop
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With