繁体   English   中英

向HTML.DropDownListFor添加一个类

[英]Adding a class to HTML.DropDownListFor

我正在尝试向以下代码行添加类分配,

        @Html.DropDownListFor(model => model.Business, new SelectList(
              new List<Object>{
                   new { ... },
                   new { ... },
                   new { ... }
                },
              "value",
              "text",
              2))

在此站点上给出的一个答案( HTML.DropDownListFor类分配 )建议将其添加为一种重载方法,该方法将“对象htmlAttributes”作为最后一个参数。 但是,我看不到此参数是一个选项,所以我有点困惑这是如何实现的。

在我的示例中,我正在使用所有可能的重载方法,而htmlattribute没有任何方法。

DropDownListFor重载中的四个将HTML属性作为其最后一个参数。 可能是最简单的。 修改您的示例,它将变为:

@Html.DropDownListFor(model => model.Business, 
                      new SelectList(
                          new List<Object>{
                              new { ... },
                              new { ... },
                              new { ... }
                          },
                          "value",
                          "text",
                          2),
                      new {
                          @class = "myclass"
                      })

使用以下重载:

public static MvcHtmlString DropDownListFor<TModel, TProperty>(
this HtmlHelper<TModel> htmlHelper,
Expression<Func<TModel, TProperty>> expression,
IEnumerable<SelectListItem> selectList,
IDictionary<string, Object> htmlAttributes
)

这是过载的MSDN文档

这样做:

@Html.DropDownListFor(model => model.Business, 
                      new SelectList(new List<Object>
                                 { 
                                  new { ... },
                                  new { ... },
                                  new { ... } 
                                }, 
                                "value", "text", 2),
                     new { @class ="YourClass"})

或以下重载也将起作用:

public static MvcHtmlString DropDownListFor<TModel, TProperty>(
this HtmlHelper<TModel> htmlHelper,
Expression<Func<TModel, TProperty>> expression,
IEnumerable<SelectListItem> selectList,
string optionLabel,
IDictionary<string, Object> htmlAttributes
)

这条路:

@Html.DropDownListFor(model => model.Business, 
                      new SelectList(new List<Object> 
                                        { 
                                         new { ... }, 
                                         new { ... },
                                        }, "value", "text", 2), 
                      null,new { @class ="YourClass"})

过载的MSDN文档

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM