Ok I've looked around at different websites regarding the use of ENUM
in DropDownLists
and am a little confused with people using Custom Helpers to pass values into a ViewBag before passing them to the DDL
.
enum values in drop down list in mvc4
I've used various Html.DropDownList
helpers throughout the website but have not used to ENUM
in a DDL
before. I'd rather not hardcode them into the View as I am using a switch
statement to control what occurs when a filter
option is selected. So was wondering if there was a simple way of implementing this without creating a new helper to remedy the situation.
Here is what I have so far (minus unnecessary code):
public enum OrderFilter
{
All,
Live,
InProgress,
Invoiced,
Outstanding
}
public ActionResult Index(OrderFilter? orderFilter)
{
ViewBag.FilterOptions = orderFilter;
}
@Html.DropDownList("orderFilter", (IEnumerable<SelectListItem>)ViewBag.FilterOptions, "Filter Options")
If I do it as I have done above, the response is "There is no ViewData item of type 'IEnumerable<SelectListItem>
' that has the key 'orderFilter
'".
I am looking for something similar to the following code, but without the use of a custom helper (if possible).
@Html.DropDownList("misc", (IEnumerable<SelectListItem>)ViewBag.Misc)
var users = _userRepository.GetData().Select(u => new SelectListItem
{
Value = u.Id.ToString(CultureInfo.InvariantCulture),
Text = u.Details
});
ViewBag.Administrators = users;