【问题标题】:default check in radiobutton list in MVC?在 MVC 中默认检查单选按钮列表?
【发布时间】:2011-03-22 15:26:18
【问题描述】:

我在单选按钮(5 个值)中传递值列表。但我希望默认选择/选中其中一个。我该怎么做?

【问题讨论】:

    标签: asp.net-mvc-2 radiobuttonlist


    【解决方案1】:

    您可以将视图模型属性设置为所需的值。示例:

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

    【讨论】:

      猜你喜欢
      • 2019-06-28
      • 1970-01-01
      • 2021-11-10
      • 2023-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-07
      • 2013-07-20
      相关资源
      最近更新 更多