Click here to Skip to main content
15,893,401 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I have applied the following code to implement asp.net mvc dropdownlist
and It is working as expected.
--------------------------------------------------------------------
C#
ViewBag.DepotID = new SelectList(TSDataBase.Depot, "DepotID", "DepotName", 2);


-----------------------------------------------------------------------
HTML
@Html.DropDownList("DepotID", "All")

------------------------------------------------------------------

But I need to add some css and javascrip to the dropdown.
I was trying as follows.

HTML
@Html.DropDownList("DepotID", "All", new { style = "width: 50px; border:1px solid #7D7D7D; border-radius:5px;", onchange = "this.form.submit();" })


But here it is showing error (ie. red uderline is appearing)
I could not get any solution for this from google also.

Can any give solution for this?
Posted
Comments
Zoltán Zörgő 15-Sep-12 4:51am    
Any progress?

It is because not all combination of parameters exist in the constructor. First you used this one: MvcHtmlString DropDownList(this HtmlHelper htmlHelper, string name, string optionLabel), afterwards you wanted to add htmlattributes, but there is no constructor overload with (string, string, object) signature.
You need to use this one: http://msdn.microsoft.com/en-us/library/dd504967(v=vs.100)[^]

C#
@Html.DropDownList("DepotID", ViewBag.DepotID, "All", new { style = "width: 50px; border:1px solid #7D7D7D; border-radius:5px;", onchange = "this.form.submit();" })
 
Share this answer
 
You need to create and add a 'class' to drodpown:
HTML
@Html.DropDownListFor(m => m.ID, Model.MyList, new { @class="selectStyle" })

so you could do the following in the css:
CSS
.selectStyle { /* whatever */ }


Do look at this article on how to add more stuff to existing select: ASP.NET MVC 3: Drop Down Lists / SelectLists[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900