简体   繁体   中英

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. 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.

Four of the DropDownListFor overloads have HTML Attributes as their last parameter. 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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