如何根据对象值选择Html.Radiobutton

时间:2012-05-29 00:50:19

标签: asp.net-mvc-3 radio-button

我在asp.net mvc编辑视图中有下列的RadioButtons: -

<div class="editor-label">
    @Html.LabelFor(model => model.InsuranceSettled)
</div>

<div class="editor-field">
    @Html.RadioButton("InsuranceSettled", 1) Yes
    @Html.ValidationMessageFor(model => model.InsuranceSettled)
</div>

<div class="editor-field">
    @Html.RadioButton("InsuranceSettled", 0) No
    @Html.ValidationMessageFor(model => model.InsuranceSettled)
</div>

但是我如何根据Model值选择所需的单选按钮,所以如果model.InsuranceSettled值为0则选择No单选按钮,反之亦然。 BR

3 个答案:

答案 0 :(得分:3)

我建议改用RadioButtonFor。我认为最好使用强类型助手来生成输入,以保证输入的名称正确。此外,他们还会根据您的型号自动选择默认值。

@Html.RadioButtonFor(model => model.InsuranceSettled, 1) 

@Html.RadioButtonFor(model => model.InsuranceSettled, 0) 

答案 1 :(得分:2)

试试这个:

@Html.RadioButton("InsuranceSettled", 1, Model.InsuranceSettled == 1)
....
@Html.RadioButton("InsuranceSettled", 0, Model.InsuranceSettled == 0)

答案 2 :(得分:0)

@Html.RadioButtonFor(model => model.InsuranceSettled , "True", model.InsuranceSettled == 1? new { Checked = "checked" } : null)

@Html.RadioButtonFor(model => model.InsuranceSettled , "False", model.InsuranceSettled == 0? new { Checked = "checked" } : null)