1

我的 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.InsuranceSettled 值为 0,则选择 No 单选按钮,反之亦然.. BR

4

3 回答 3

3

我建议RadioButtonFor改用。我认为最好使用强类型帮助器来生成您的输入,以确保您的输入具有正确的名称。他们还将根据您的模型自动选择默认值。

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

@Html.RadioButtonFor(model => model.InsuranceSettled, 0) 
于 2012-05-29T06:12:52.773 回答
2

试试这个:

@Html.RadioButton("InsuranceSettled", 1, Model.InsuranceSettled == 1)
....
@Html.RadioButton("InsuranceSettled", 0, Model.InsuranceSettled == 0)
于 2012-05-29T02:02:02.677 回答
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)
于 2012-05-29T08:00:38.967 回答