In a view I have two <input type="radio">
, combine they make a radio group.
I want to bind them with a field of Model, in a way if value is true it should bind first radio button if false it should bind the other.
Please guide how it can be achived. I tried below code but it always checked the second radio no matter what value model has.
<div class="radiobuttons">
<input type="radio" name="LenderType" checked="@Model.Filter_Value" id="rbtnAllLenders" class="cb">
<input type="radio" id="rbtnMajorLendersOnly" checked="[email protected]_Value" name="LenderType" class="cb">
</div>
You manual code is adding the
selected
attribute to both radio buttons. All the following are equivalent:Note the last 2 are invalid values for the
checked
attribute, but still check the radio button. Since you have a group name, and only one button can be visually checked, the last one rendered is selected. Refer W3C specifications.Use html helpers to bind to your model. If the model contains property
string LenderType
thenIf the value of
LenderType
is "All lenders" the first option will be selected. If the value is "Major lenders", then the second option will be selected