Ethan Pelton
Ethan Pelton

Reputation: 1796

asp.net mvc static selectlist

Still trying to rap my head around mvc/EF/razor. I have a static/hard-coded drop down list. I want to "bind" this to a non-enumerable model entity "status".

In the view I have

@Html.DropDownList("status", String.Empty)

In the controller, I have

ViewBag.status = new SelectList(new[] { "yes", "no", "maybe" }, booking.status);

I suspect if status was enumerable, I would be done, but I get the following error since status is not enumerable.

There is no ViewData item of type IEnumerable<SelectListItem> that has the key 'status'.

I'm clearly misssing some things and greatly appreciate any help.

Upvotes: 0

Views: 701

Answers (1)

Moho
Moho

Reputation: 16553

I don't like your array initialization - that compiles? Try this:

ViewBag.status = new SelectList(new string[] { "yes", "no", "maybe" }, booking.status);

You need to pass the select list to the HtmlHelper.DropDownList method call (MSDN) like:

@Html.DropDownList("status", ( SelectList )ViewBag.status)

Upvotes: 1

Related Questions