Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: MVC
Hi
I have applied the following code to implement asp.net mvc dropdownlist
and It is working as expected.
--------------------------------------------------------------------
ViewBag.DepotID = new SelectList(TSDataBase.Depot, "DepotID", "DepotName", 2);
 
-----------------------------------------------------------------------
@Html.DropDownList("DepotID", "All")
------------------------------------------------------------------
 
But I need to add some css and javascrip to the dropdown.
I was trying as follows.
 
@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 13-Sep-12 18:54pm
Comments
Zoltán Zörgő at 15-Sep-12 4:51am
   
Any progress?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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)[^]
 
@Html.DropDownList("DepotID", ViewBag.DepotID, "All", new { style = "width: 50px; border:1px solid #7D7D7D; border-radius:5px;", onchange = "this.form.submit();" })
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

You need to create and add a 'class' to drodpown:
@Html.DropDownListFor(m => m.ID, Model.MyList, new { @class="selectStyle" })
so you could do the following in the 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[^]
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 6,631
1 Sergey Alexandrovich Kryukov 6,345
2 CPallini 5,230
3 George Jonsson 3,574
4 Gihan Liyanage 2,522


Advertise | Privacy | Mobile
Web03 | 2.8.140921.1 | Last Updated 14 Sep 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100