将@ Html.RadioButtonFor设置为默认选中

min*_*iya 5 asp.net-mvc radiobuttonfor razor asp.net-mvc-3

我无法将默认单选按钮设置为"已选中"!我正在使用@ Html.RadioButtonFor:

<div id="private-user">
    @Html.RadioButtonFor(m => m.UserType, "type1", new { @class="type-radio" , **@Checked="checked"** }) 1
</div>
<div id="proff-user">
    @Html.RadioButtonFor(m => m.UserType, "type2", new { @class="type-radio" }) 2
</div>
Run Code Online (Sandbox Code Playgroud)

是否可以使用@ Html.RadioButtonFor设置单选按钮?

谢谢

Dar*_*rov 5

在控制器操作中UserType,将视图模型上的属性值设置为相应的值:

public ActionResult SomeAction()
{
    MyViewModel model = ...

    // preselect the second radio button
    model.UserType = "type2";
    return View(model);
}
Run Code Online (Sandbox Code Playgroud)

在你看来:

<div id="private-user">
    @Html.RadioButtonFor(m => m.UserType, "type1", new { @class="type-radio" }) 1
</div>
<div id="proff-user">
    @Html.RadioButtonFor(m => m.UserType, "type2", new { @class="type-radio" }) 2
</div>
Run Code Online (Sandbox Code Playgroud)