Question

Hi I have a ModelBinded View

foreach (var Model in Model)
        {
@Html.RadioButtonFor(modelItem => Model.DefaultLocation, Model.AddressID, new { @Checked = Model.DefaultLocation, id = Model.AddressID })
}

@Checked is retrieved from Database as Boolean True or False.

HTML generated for this Razor code is as Below

<input checked="True" id="27" name="model.DefaultLocation" type="radio" value="27">
<input checked="False" id="28" name="Model.DefaultLocation" type="radio" value="28">

Though it says id="27" as Checked= "true" my page is showing the last radio button as selected.

I am trying to achieve that what ever the return value form the database says true, that Radio button should be selected by default.

I am not able to figure out whats wrong. can any one help me to fix this issue?

thank you for your time.

Was it helpful?

Solution 2

@Html.EditorFor( modelItem => Model.DefaultLocation, 
                 Model.AddressID,
                 new { @Checked = Model.DefaultLocation, id = Model.AddressID }
               )

OTHER TIPS

I know this is a old thread, but just had the same problem.

RadioButtonFor is the right way to go, but instead of

new { @Checked = Model.DefaultLocation, id = Model.AddressID }

just use

new { @isChecked = Model.DefaultLocation, id = Model.AddressID }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top