我正在传递radiobutton中的值列表(5个值)。但我希望默认选择/检查其中一个。我怎么能这样做?
答案 0 :(得分:5)
您可以将视图模型属性设置为所需的值。例如:
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
按钮。