Reputation: 1335
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.
Upvotes: 2
Views: 5044
Reputation: 62488
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"})
Upvotes: 2
Reputation: 15237
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"
})
Upvotes: 4