简体   繁体   English

向HTML.DropDownListFor添加一个类

[英]Adding a class to HTML.DropDownListFor

I am trying to add a class assignment to the following line of code, 我正在尝试向以下代码行添加类分配,

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

One answer given on this site ( HTML.DropDownListFor class assignment ) suggested adding it as an overload method that takes a 'object htmlAttributes' as the last parameter. 在此站点上给出的一个答案( HTML.DropDownListFor类分配 )建议将其添加为一种重载方法,该方法将“对象htmlAttributes”作为最后一个参数。 However, I can not see this parameter as an option so I'm a bit confused how this is possible. 但是,我看不到此参数是一个选项,所以我有点困惑这是如何实现的。

In my example I am using every overload method possible and there is none for htmlattributes. 在我的示例中,我正在使用所有可能的重载方法,而htmlattribute没有任何方法。

Four of the DropDownListFor overloads have HTML Attributes as their last parameter. DropDownListFor重载中的四个将HTML属性作为其最后一个参数。 This one is probably the easiest. 可能是最简单的。 Modifying your example, it becomes: 修改您的示例,它将变为:

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

Use following overload: 使用以下重载:

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

Here is MSDN docs of the overload 这是过载的MSDN文档

do this way: 这样做:

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

or following overload will also work: 或以下重载也将起作用:

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

this way: 这条路:

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

MSDN docs of the overload 过载的MSDN文档

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

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