I'm trying to use the Html.DropDownList
extension method but can't figure out how to use it with an enumeration.
Let's say I have an enumeration like this:
public enum ItemTypes
{
Movie = 1,
Game = 2,
Book = 3
}
How do I go about creating a dropdown with these values using the Html.DropDownList
extension method?
Or is my best bet to simply create a for loop and create the Html elements manually?
In ASP.NET MVC 5.1, they added the
EnumDropDownListFor()
helper, so no need for custom extensions:Model:
View:
Using Tag Helper (ASP.NET MVC 6):
I bumped into the same problem, found this question, and thought that the solution provided by Ash wasn't what I was looking for; Having to create the HTML myself means less flexibility compared to the built-in
Html.DropDownList()
function.Turns out C#3 etc. makes this pretty easy. I have an
enum
calledTaskStatus
:This creates a good ol'
SelectList
that can be used like you're used to in the view:The anonymous type and LINQ makes this so much more elegant IMHO. No offence intended, Ash. :)
The best solution I found for this was combining this blog with Simon Goldstone's answer.
This allows use of the enum in the model. Essentially the idea is to use an integer property as well as the enum, and emulate the integer property.
Then use the [System.ComponentModel.Description] attribute for annotating the model with your display text, and use an "EnumDropDownListFor" extension in your view.
This makes both the view and model very readable and maintainable.
Model:
View:
Extension (directly from Simon Goldstone's answer, included here for completeness):
This is version for Razor:
I am very late on this one but I just found a really cool way to do this with one line of code, if you are happy to add the Unconstrained Melody NuGet package (a nice, small library from Jon Skeet).
This solution is better because:
So, here are the steps to get this working:
Add a property on your model like so:
Now that you have the List of SelectListItem exposed on your model, you can use the @Html.DropDownList or @Html.DropDownListFor using this property as the source.
I know I'm late to the party on this, but thought you might find this variant useful, as this one also allows you to use descriptive strings rather than enumeration constants in the drop down. To do this, decorate each enumeration entry with a [System.ComponentModel.Description] attribute.
For example:
Here is my code:
You can then do this in your view:
Hope this helps you!
**EDIT 2014-JAN-23: Microsoft have just released MVC 5.1, which now has an EnumDropDownListFor feature. Sadly it does not appear to respect the [Description] attribute so the code above still stands.See Enum section in Microsoft's release notes for MVC 5.1.
Update: It does support the Display attribute
[Display(Name = "Sample")]
though, so one can use that.[Update - just noticed this, and the code looks like an extended version of the code here: https://blogs.msdn.microsoft.com/stuartleeks/2010/05/21/asp-net-mvc-creating-a-dropdownlist-helper-for-enums/, with a couple of additions. If so, attribution would seem fair ;-)]