【问题标题】:How to Set RadioButtonFor() in ASp.net MVC 2 as Checked by default如何在 ASp.net MVC 2 中将 RadioButtonFor() 设置为默认选中
【发布时间】:2011-02-05 14:53:35
【问题描述】:

如何将 RadioButtonFor() 设置为默认选中

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

(Html.RadioButton) 有出路,但 (Html.RadioButtonFor) 没有出路

有什么想法吗?

【问题讨论】:

  • 使用 RadioButtonFor() 时是否没有默认检查选项

标签: asp.net-mvc-2 radiobuttonfor


【解决方案1】:

使用简单的方法:

<%= Html.RadioButtonFor(m => m.Gender, "Male", new { Checked = "checked" })%>

【讨论】:

  • 谢谢!也可以是new { @checked = "checked" }
  • 我可以在此处将 Checked 属性绑定到模型的布尔属性吗?这样当我单击它时,布尔属性变为 true,我可以将该 true 值绑定到操作方法?
【解决方案2】:

这不是太漂亮,但如果您只需要为整个网站实现很少的单选按钮,那么这样的选项也可能是一种选择:

&lt;%=Html.RadioButtonFor(m =&gt; m.Gender,"Male",Model.Gender=="Male" ? new { @checked = "checked" } : null)%&gt;

【讨论】:

    【解决方案3】:

    我假设您应该有一组单选按钮。可能是这样的

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

    您可以从控制器中为 m.Gender = "Unknown"(或其他内容)提供默认值。

    【讨论】:

    • 但我只想要两个单选按钮,其中一个默认使用 Html.RadiobuttonFor() 进行检查
    • 我也为此苦苦挣扎,但发现这个答案很简单。如果你在你的模型上创建一个属性,比如说性别,在你的模型的默认构造函数中,只需将该属性设置为默认性别,比如Gender = "Male";。如果这样做,mvc.net 将自动选择默认值,并且在回发时会自动将所选值绑定到您的 Gender 属性。
    【解决方案4】:

    This question on StackOverflow 处理 RadioButtonListFor 并且答案也解决了您的问题。您可以在 RadioButtonListViewModel 中设置 selected 属性。

    【讨论】:

      【解决方案5】:

      这个 Helper 计算表达式,如果等于它检查单选按钮的值,并且具有与 RadioButtonFor 相同的参数(因此名称不同):

      public static MvcHtmlString CheckedRadioButtonFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, object value)
      {
          return CheckedRadioButtonFor(htmlHelper, expression, value, null);
      }
      
      public static MvcHtmlString CheckedRadioButtonFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, object value, object htmlAttributes)
      {
          var func = expression.Compile();
          var attributes = new RouteValueDictionary(htmlAttributes);
          if ((object)func(htmlHelper.ViewData.Model) == value) {
              attributes["checked"] = "checked";
          }
          return htmlHelper.RadioButtonFor(expression, value, attributes);
      }
      

      用法:

      <%= Html.CheckedRadioButtonFor(m => m.Gender, "Male", new { id = "gender-male" })%>
      

      结果:

      <!-- For Model.Gender = "Male" -->
      <input checked="checked" id="gender-male" name="Gender" type="radio" value="Male">
      <!-- For Model.Gender = "Female" -->
      <input id="gender-male" name="Gender" type="radio" value="Male">
      

      【讨论】:

      • 您应该使用HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes); 而不是new RouteValueDictionary(htmlAttributes),以免丢失data_somethingdata-something 的自动转换
      【解决方案6】:

      我找到了另一个选项,因此您可以将 @Html.EditorFor() 与模板一起使用:

      假设我有这个枚举:

      public enum EmailType { Pdf, Html }
      

      我可以把这段代码放在 Views/Shared/EditorTemplates/EmailType.cshtml

      @model EmailType
      @{
          var htmlOptions = Model == EmailType.Html ? new { @checked = "checked" } : null;
          var pdfOptions = Model == EmailType.Pdf ? new { @checked = "checked" } : null;
      }
      
      @Html.RadioButtonFor(x => x, EmailType.Html, htmlOptions) @EmailType.Html.ToString()
      @Html.RadioButtonFor(x => x, EmailType.Pdf, pdfOptions) @EmailType.Pdf.ToString()
      

      如果我想随时使用它,我可以简单地使用它:

      @Html.EditorFor(x => x.EmailType)
      

      这种方式更通用,而且我觉得更容易改变。

      【讨论】:

        【解决方案7】:

        您还可以添加与具有相同 ID 的单选按钮相关联的标签,然后允许用户单击单选按钮或标签来选择该项目。我在这里为“男性”、“女性”和“未知”使用常量,但显然这些可能是模型中的字符串。

        <%: Html.RadioButtonFor(m => m.Gender, "Male", 
            new Dictionary<string, object> { { "checked", "checked" }, { "id", "Male" } }) %>
        <%: Html.Label("Male") %>
        
        <%: Html.RadioButtonFor(m => m.Gender, "Female", 
            new Dictionary<string, object> { { "id", "Female" } }) %>
        <%: Html.Label("Female")%>
        
        <%: Html.RadioButtonFor(m => m.Gender, "Unknown",
            new Dictionary<string, object> { { "id", "Unknown" } }) %>
        <%: Html.Label("Unknown")%>
        

        【讨论】:

        • 谢谢!在为复选框创建标签时遇到困难。现在很明显,但无法弄清楚!
        【解决方案8】:

        遇到这个,我想我会指出,对于 MVC 5,您需要做的就是在模型上设置值。例如:

        型号:

           public class ExampleModel
            {
                public PackingListInputModel()
                {
        
                     RadioButtonField = "One";
        
                }
                public string RadioButtonField { get; set; }
            }
        

        查看:

        @model ExampleModel
        
        @using (Html.BeginForm)
        {
            @Html.RadioButtonFor(m => m.RadioButtonField , "One")
            @Html.RadioButtonFor(m => m.RadioButtonField , "Two")
        }
        

        第一个单选按钮(“One”)的状态将设置为活动状态,因为该值与模型中设置的值相匹配。

        【讨论】:

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

          @checked = checked
          

          如果你喜欢

          【讨论】:

            【解决方案10】:

            如果您使用的是 jquery,则可以在单选按钮之前调用它。

            $('input:radio:first').attr('checked', true);
            

            ^ 这将选中第一个单选框,但您可以查看更多 jquery 以循环到您想要选择的那个。

            【讨论】:

              【解决方案11】:
                         @Html.RadioButton("Insured.GenderType", 1, (Model.Insured.GenderType == 1 ))
                         @Web.Mvc.Claims.Resources.PartyResource.MaleLabel
                         @Html.RadioButton("Insured.GenderType", 2, Model.Insured.GenderType == 2)
                         @Web.Mvc.Claims.Resources.PartyResource.FemaleLabel
              

              【讨论】:

                【解决方案12】:

                这是将默认单选按钮设置为 true 的代码

                @Html.RadioButtonFor(m => m.Gender, "Male", new { @checked = "checked", id = "rdGender", name = "rbGender" })
                

                【讨论】:

                  【解决方案13】:

                  我发现最好将默认值放在模型的构造方法中。

                  Gender = "Male";
                  

                  【讨论】:

                    【解决方案14】:

                    如果单选按钮的值与 Model.Gender 值匹配,则需要在 RadioButtonFor 中添加 'checked' htmlAttribute。

                    @{
                            foreach (var item in Model.GenderList)
                            {
                                <div class="btn-group" role="group">
                                   <label class="btn btn-default">
                                       @Html.RadioButtonFor(m => m.Gender, item.Key, (int)Model.Gender==item.Key ? new { @checked = "checked" } : null)
                                       @item.Value
                                  </label>
                                </div>
                            }
                        }
                    

                    完整代码见以下链接:渲染引导单选按钮组,默认选中。 stackoverflow answer link

                    【讨论】:

                      猜你喜欢
                      • 2014-06-16
                      • 2011-02-05
                      • 1970-01-01
                      • 2013-07-13
                      • 1970-01-01
                      • 2011-03-30
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      相关资源
                      最近更新 更多