user3611792
user3611792

Reputation:

RadiobuttonFor in Mvc Razor syntax

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
@if (Model.IsReady == true)
{
    <span> @Html.RadioButtonFor(model => model.StatusId,"1") Active</span>
}
@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 got 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
@if (Model.IsReady == true)
{
    <span> @Html.RadioButtonFor(model => model.StatusId, "1",Model.StatusId == '1' ? new {Checked = "checked"} : null) Active</span>
}
@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

Upvotes: 15

Views: 37353

Answers (1)

matt90410
matt90410

Reputation: 465

The way I create radiobuttons is as follows:

@Html.RadioButtonFor(model => model.StatusId,3,new { id = "rbMaintenance" })
@Html.Label("rbMaintenance","Maintenance")
@Html.RadioButtonFor(model => model.StatusId,2,new { id = "rbInactive" })
@Html.Label("rbInactive","Inactive")
@Html.RadioButtonFor(model => model.StatusId,1,new { id = "rbActive" })
@Html.Label("rbActive","Active")

The difference to your code really is that as your StatusId type is Int then you should enter the value in the RadioButtonFor as an integer e.g. 3 for Maintenance rather than '3'.

Upvotes: 28

Related Questions