开发者

Model Binding RadioButtonFor() default Select MVC3.0?

开发者 https://www.devze.com 2023-04-08 01:01 出处:网络
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 }

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 b开发者_如何学Goutton 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.


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 }


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号