Q: How to set width for @Html.DropDownList (and not in css)?
@Html.DropDownList("ListId", String.Empty, new {style="width: 250px;"}) @* no go!*@
Q: How to set width for @Html.DropDownList (and not in css)?
@Html.DropDownList("ListId", String.Empty, new {style="width: 250px;"}) @* no go!*@
The second argument of the DropDownList helper must be an
IEnumerable<SelectListItem>
. You are passing a string (an empty one to be more precise). So in order to use this helper you will have to respect its signature:Obviously generating an empty dropdown list is of little use. You probably have a view model (you should by the way) that you want to use to bind to:
and of course because you have a view model you should prefer to use the
DropDownListFor
helper:and finally to avoid cluttering your HTML with styles you should use an external CSS:
where in your external CSS you would define the
.mycombo
rule:Now you have what I consider a proper code.
There is a jquery technique that allows you to set the width without having to deal with the @Html.DropDownList constructor.
You should use the View Model approach. However the lazy way out is just to give the 2nd parameter a null.