Mark Redman
Mark Redman

Reputation: 24515

ASP.NET MVC SelectList: How do I set class name and other attributes?

How can I set a select class and other attributes eg:events through the SelectList object?

If this cannot be done, what are the best practices in populating a select input (dropdownlist) ie should I just loop through the items and render the <option> tags

I guess events should be bound to the controls in jquery for instance, but I do need the class forto do this.

Upvotes: 1

Views: 1052

Answers (2)

Gary W
Gary W

Reputation: 1904

The htmlAttributes parameter should take an anonymous type initialised like this :

new {@class="some-css-class"}

Upvotes: 3

Gregoire
Gregoire

Reputation: 24832

<%=Html.DropDownList("elementname",yourSelectList,"defaultvalue",new{@class="yourclass"}) %>

Upvotes: 1

Related Questions