williamsandonz
williamsandonz

Reputation: 16460

ASP.NET MVC Showing previously selected radio (when 2x radiobuttonfor exist)

I have:

<label class="radio inline">
@Html.RadioButtonFor(model => model.ViewModelForCar.CarTypeID, CarType.CoolTypeID) Cool car
</label>
<label class="radio inline">
@Html.RadioButtonFor(model => model.ViewModelForCar.CarTypeID, CarType.LameTypeID) Lame car
</label>

But when a form is submitted, and result invalid, and the page re-loads, the wrong radio the first radio is always marked as selected, even if I marked the second one as selected. I'm not keen on using radiobuttonlist because I have custom markup

Upvotes: 0

Views: 50

Answers (1)

heads5150
heads5150

Reputation: 7463

MVC does not how to rebind the model back because you have two radio buttons named the same.

Modifying the radio buttons to

<label class="radio inline">
@Html.RadioButtonFor(model => model.ViewModelForCar.CoolTypeID, CarType.CoolTypeID) Cool car
</label>
<label class="radio inline">
@Html.RadioButtonFor(model => model.ViewModelForCar.LameTypeID, CarType.LameTypeID) Lame car
</label>

will give you two distinct radio buttons. Then you can handle working which radio button is selected in the controller.

Upvotes: 1

Related Questions