I am new in MVC razor. I am trying to edit the web page. In which I have two radio buttons. I am successful in save data for radio button values. But when I trying to edit that value I am not able to select that radio button for which data is saved. I have enum for Gender
public enum Gender { Male, Female }
My create code is :-
<div class="editor-label"> @Html.LabelFor(model => model.gender) </div> <div class="editor-field"> @Html.RadioButtonFor(x => x.gender, (int)Gender.Male) Male @Html.RadioButtonFor(x => x.gender, (int)Gender.Female) Female </div>
Edit code is like
<div class="editor-label"> @Html.LabelFor(model => model.gender) </div> <div> @if (Model.gender == (int)Gender.Male) { @Html.RadioButtonFor(model => model.gender, "Male", new { @checked = true }) @Html.Label("Male") @Html.RadioButtonFor(model => model.gender, "Female") @Html.Label("Female") } else { @Html.RadioButtonFor(model => model.gender, "Male") @Html.Label("Male") @Html.RadioButtonFor(model => model.gender, "Female", new { @checked = true }) @Html.Label("Female") } </div>
You have written like
@Html.RadioButtonFor(model => model.gender, "Male", new { @checked = true }) and @Html.RadioButtonFor(model => model.gender, "Female", new { @checked = true })
Here you have taken gender as a Enum
type and you have written the value for the radio button as a string
type- change "Male" to 0 and "Female" to 1.
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