Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Wednesday, April 17, 2013 9:13 AM
Hello- how to add class to dropdown list
@Html.DropDownList(name, null , optionList, selectedValue, null)
Thanks
All replies (8)
Wednesday, April 17, 2013 12:55 PM ✅Answered
ssvi...
It would be helpful if you were to tell us what you are trying to accomplich. The Html.Dropdownlist has several overloaded methods, (see http://msdn.microsoft.com/en-us/library/gg547850(v=vs.111).aspx) and one cannot tell which one you are trying to use in your example.
Several of those overloads allow ytou to specify an Object which is a dictionary of desired attributes (e.g., new{@class="xxx"} - note the "@" is necessary because class is a reserved word).
Review the methods at http://msdn.microsoft.com/en-us/library/gg547850(v=vs.111).aspx, identify those that allow an "object" as a final parameter, find the description that most meets your requirements, and code your statement in accordance with the format of the selected overloaded method.
Wednesday, April 17, 2013 1:00 PM ✅Answered
You'll need to make sure that you are passing all of the appropriate values and types into the DropDownList method :
<!-- The Constructor you are looking for will accept these basic parameters -->
@Html.DropDownList((ElementName), (Collection of SelectListItems), (Anonymous HTML Attributes))
Give the following a try :
@Html.DropDownList(name, new SelectList(optionList,selectedValue),new { @class = "xx" })
Notice that the second parameter actually creates a SelectList that is populated with two arguments an actual IEnumerable (your optionList) and a SelectedValue parameter (your selectedValue).
Wednesday, April 17, 2013 9:30 AM
See http://stackoverflow.com/questions/7367344/adding-a-css-class-to-select-using-html-dropdownlist
Wednesday, April 17, 2013 9:31 AM
@Html.DropDownList(name, null , optionList, selectedValue, null,new { @class="ClassName" })
Wednesday, April 17, 2013 10:12 AM
Hello diplomarames...
I tried your code but throws an error
Compiler Error Message: CS1501: No overload for method 'DropDownList' takes 6 arguments
Source Error:
|
Wednesday, April 17, 2013 10:23 AM
Sorry for that . Please try this one
@Html.DropDownList(name,optionList,selectedValue, new { @class = "xx" })
Wednesday, April 17, 2013 10:35 AM
I have tried it again showing the below error
Compiler Error Message: CS1502: The best overloaded method match for 'System.Web.WebPages.Html.HtmlHelper.DropDownList(string, string, System.Collections.Generic.IEnumerable<System.Web.WebPages.Html.SelectListItem>, System.Collections.Generic.IDictionary<string,object>)' has some invalid arguments
Source Error:
|
Wednesday, April 17, 2013 11:19 AM
I'm not sure of your target.
As you can see from its reference page, the HtmlHelper.DropDownList Method (String, String, IEnumerable<SelectListItem>, Object) expects an object that "contains custom attributes for the element. The attribute names and values are retrieved through reflection by examining the properties of the object".
Are you looking for this?