【问题标题】:how to select a radio button by default - asp.net mvc strongly typed html helpers如何默认选择单选按钮 - asp.net mvc 强类型 html 助手
【发布时间】:2011-03-10 03:04:57
【问题描述】:

我有一个像这样的单选按钮列表:

<%=Html.RadioButtonFor(m => m.Gender,"Male")%>

我希望默认选中此按钮。我该怎么做?

【问题讨论】:

    标签: html asp.net-mvc html-helper


    【解决方案1】:
    <%: Html.RadioButtonFor(x => x.Gender, "Male", new { @checked = "checked" }) %>
    

    或在呈现此视图的控制器操作中:

    model.Gender = "Male";
    return View(model);
    

    【讨论】:

    • 对不起,达林,这不起作用。有其他解决方案吗?
    • 当然可以,我刚刚创建了一个示例 ASP.MVC 项目,放置了一个具有单个属性 Gender(字符串类型)的视图模型,并在视图中使用了 HTML 属性设置checked 属性。
    【解决方案2】:

    如果你使用 Razor view strong,你可以使用这样的单选按钮

      @Html.RadioButtonFor(model => model.PrintOrder, "Sequential", new {@checked="true"})    Sequential  
    

    根据您的需要将其更正为 剃刀视图

      @Html.RadioButtonFor(x => x.Gender, "Male", new { @checked = "checked" })
    

    aspx 视图

      <%: Html.RadioButtonFor(x => x.Gender, "Male", new { @checked = "checked" }) %>
    

    【讨论】:

      【解决方案3】:

      试一试:

      <%: Html.RadioButtonFor(x => x.Gender, "Male", new { Checked = true }) %>
      

      在我的情况下工作。

      【讨论】:

        猜你喜欢
        • 2012-05-25
        • 2013-09-17
        • 2013-03-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多