Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to set radio button checked in edit mode in MVC razor view

Tags:

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> 
like image 581
sushama Avatar asked Aug 08 '14 05:08

sushama


Video Answer


1 Answers

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.

like image 174
RohannG Avatar answered Oct 24 '22 10:10

RohannG