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
You need to make a few changes. First, change your controller code from this:
To this:
Second, change your helper to this:
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.