【发布时间】:2011-03-22 15:26:18
【问题描述】:
我在单选按钮(5 个值)中传递值列表。但我希望默认选择/选中其中一个。我该怎么做?
【问题讨论】:
标签: asp.net-mvc-2 radiobuttonlist
我在单选按钮(5 个值)中传递值列表。但我希望默认选择/选中其中一个。我该怎么做?
【问题讨论】:
标签: asp.net-mvc-2 radiobuttonlist
您可以将视图模型属性设置为所需的值。示例:
public class MyViewModel
{
public string Value { get; set; }
}
public class HomeController : Controller
{
public ActionResult Index()
{
var model = new MyViewModel { Value = "No" };
return View(model);
}
}
在视图中:
<%= Html.RadioButtonFor(x => x.Value, "Yes", new { id = "yes" }) %> Yes
<%= Html.RadioButtonFor(x => x.Value, "No", new { id = "no" }) %> No
<%= Html.RadioButtonFor(x => x.Value, "Maybe", new { id = "maybe" }) %> Maybe
这将选择No 按钮。
【讨论】: