默认检查MVC中的radiobutton列表?

时间:2011-03-21 18:26:32

标签: asp.net-mvc-2 radiobuttonlist

我正在传递radiobutton中的值列表(5个值)。但我希望默认选择/检查其中一个。我怎么能这样做?

1 个答案:

答案 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按钮。