Asp.net mvc 将@Html.radiobutton设置为默认选中状态
我无法将默认单选按钮设置为“选中”! 我正在使用@Html.radiobutton:Asp.net mvc 将@Html.radiobutton设置为默认选中状态,asp.net-mvc,asp.net-mvc-3,razor,radiobuttonfor,Asp.net Mvc,Asp.net Mvc 3,Razor,Radiobuttonfor,我无法将默认单选按钮设置为“选中”! 我正在使用@Html.radiobutton: <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
<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>
@(m=>m.UserType,“type1”,新的{@class=“type radio”,***@Checked=“Checked”**})1
@(m=>m.UserType,“type2”,新的{@class=“type radio”})2
是否可以使用@Html.radiobutton将单选按钮设置为选中状态
Thx请查看此处或列表
如果您使用的是枚举,请查看此答案查看此帖子,他们已经回答了您的问题(尽管是MVC 2,但在本文中是相同的):
希望这有助于在控制器操作中将视图模型上的
UserType
属性的值设置为相应的值:
public ActionResult SomeAction()
{
MyViewModel model = ...
// preselect the second radio button
model.UserType = "type2";
return View(model);
}
在你看来:
<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>
@(m=>m.UserType,“type1”,新的{@class=“type radio”})1
@(m=>m.UserType,“type2”,新的{@class=“type radio”})2
在视图模型的构造函数中,只需将属性初始化为所需的值
public YourViewModel(){
UserType = "type1";
}
这样,您的回发将使用您想要的任何值,并且您不必在控制器中设置该值。即使在我的例子中,UserType是“enum”。我是否应该将其转换为封装enum用户类型的其他类型的对象?