Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can get selected radion button in razor

I have three radio buttons and my field value type is integer like Maintenance for 3, Active for 1 and Inactive for 2.

     @Html.RadioButtonFor(model => model.StatusId, "3") Maintenance
     @Html.RadioButtonFor(model => model.StatusId,"1") Active
     @Html.RadioButtonFor(model => model.StatusId, "2") Inactive

i using above code then insert data properly but when my form open in edit mode then i have not get any radio button selected.

and i also used below code but not get success.

  @Html.RadioButtonFor(model => model.StatusId, "3", Model.StatusId == '3' ? new {Checked = "checked"} : null) Maintenance
  @Html.RadioButtonFor(model => model.StatusId, "1",Model.StatusId == '1' ? new {Checked = "checked"} : null) Active}
  @Html.RadioButtonFor(model => model.StatusId, "2",Model.StatusId == '2' ? new {Checked = "checked"} : null) Inactive

so how to get selected radio button in edit mode

thanks in advance

like image 961
Vinit Patel Avatar asked Feb 14 '23 01:02

Vinit Patel


1 Answers

Please try with the below code snippet.

@Html.RadioButtonFor(model => model.StatusId, "2", Model.StatusId == '2' ? new {@Checked = "checked"} : '') 

OR

@Html.RadioButtonFor(model => model.StatusId, "2", Model.StatusId == '2' ? new {@Checked = "checked"} : null) 

If above code is not worked then please provide your rendered html code of this radiobutton/input.

like image 136
Jayesh Goyani Avatar answered Feb 23 '23 04:02

Jayesh Goyani