Reputation: 7818
Thanks to help I got here - I have the following code in my controller:
var stands = db.Stands.ToList().Where(s => s.ExhibitorID == null)
.Select(s => new SelectListItem
{
Value = s.StandID.ToString(),
Text = s.Description + "-- £" + s.Rate.ToString()
});
ViewBag.StandID = new SelectList(stands, "Value", "Text");
And render it in my view like this:
<div class="editor-field">
@Html.DropDownList("StandID", new {style = "width:150px"})
</div>
However in the view, I get the following error:
CS1928: 'System.Web.Mvc.HtmlHelper' does not contain a definition for 'DropDownList' and the best extension method overload 'System.Web.Mvc.Html.SelectExtensions.DropDownList(System.Web.Mvc.HtmlHelper, string, string)' has some invalid arguments
It's fine when I remove the new {style=....
How can I set the width of the dropdownlist, without getting this error?
Thank you,
Mark
Upvotes: 0
Views: 2585
Reputation: 93474
You need to make a few changes. First, change your controller code from this:
ViewBag.StandID = new SelectList(stands, "Value", "Text");
To this:
ViewBag.StandList = new SelectList(stands, "Value", "Text");
Second, change your helper to this:
@Html.DropDownList("StandID", (SelectList)ViewBag.StandList, new {style = "width:150px"})
It's important to make sure the html elements name is different from the collection of items, otherwise all kinds of problems occur.
It's even better to use a strongly typed view model.
@Html.DropDownListFor(m => m.StandID, Model.StandList, new {style="width:150px;"})
Upvotes: 3
Reputation: 4803
You're not passing in the correct parameters to @Html.DropDownList. From the MSDN documentation here: http://msdn.microsoft.com/en-us/library/dd470380(v=vs.100).aspx
It looks like you want to use the following overload:
public static MvcHtmlString DropDownList(
this HtmlHelper htmlHelper,
string name,
IEnumerable<SelectListItem> selectList,
Object htmlAttributes
)
So, your first parameter is the name string which you have correct, but then you need the SelectList as your second parameter and your HtmlAttributes as your third. Try it like this:
<div class="editor-field">
@Html.DropDownList("StandID", ViewBag.StandID, new {style = "width:150px"})
</div>
UPDATE:
Not sure your are passing the correct thing into your ViewBag either. You're setting it equal to a new SelectList object and the DropDownList needs a collection of SelectListItems.
Try this in your controller:
var stands = db.Stands.ToList().Where(s => s.ExhibitorID == null)
.Select(s => new SelectListItem
{
Value = s.StandID.ToString(),
Text = s.Description + "-- £" + s.Rate.ToString()
});
ViewBag.StandID = stands;
UPDATE:
Here is how I accomplish this same thing. I have a static method that returns an IEnumerable and then I reference that method in my view. (Sorry for VB syntax)
Namespace Extensions
Public Module Utilities
Public Function SalutationSelectList(Optional ByVal Salutation As String = "") As IEnumerable(Of SelectListItem)
Dim ddl As New List(Of SelectListItem)
ddl.Add(New SelectListItem With {.Text = "", .Value = "", .Selected = If(Salutation = "", True, False)})
ddl.Add(New SelectListItem With {.Text = "Mr.", .Value = "Mr.", .Selected = If(Salutation = "Mr.", True, False)})
ddl.Add(New SelectListItem With {.Text = "Ms.", .Value = "Ms.", .Selected = If(Salutation = "Ms.", True, False)})
ddl.Add(New SelectListItem With {.Text = "Mrs.", .Value = "Mrs.", .Selected = If(Salutation = "Mrs.", True, False)})
ddl.Add(New SelectListItem With {.Text = "Dr.", .Value = "Dr.", .Selected = If(Salutation = "Dr.", True, False)})
Return ddl
End Function
End Module
End Namespace
@Html.DropDownListFor(Function(org) org.Salutation, SalutationSelectList())
Upvotes: 1