A collection of HTML helpers that generate DropDownlists for enums, with or without localization support.
Table of contents
HTML Helpers Overview
I've created a set of HTML helpers that generate a DropDownList for an enum.
Those helpers are similar to SelectExtensions.DropDownList Method and
SelectExtensions.DropDownListFor Method ,
with the only difference being that the those helpers will populate the DropDownList with the elements of the specified enum.
Some examples - Basic usage
Let's assume the following model:
[sourcecode language="csharp"]
public enum WeekDay
{
Sunday,
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday
}
public class WeeklyEvent
{
public string Title { get; set; }
public WeekDay Day { get; set; }
public WeekDay? AnotherDay { get; set; }
}
[/sourcecode]
Setting the name of the element and default empty item text
This is the most basic usage, the generated text will be enumValue.ToString():
[sourcecode language="html"]
@Html.EnumDropDownList("eventDay", "Select an item") // name="eventDay"
@Html.EnumDropDownListFor(x => x.Day, "Select an item") // name="Day"
[/sourcecode]
Html.EnumDropDownListFor works with nullables too:
[sourcecode language="html"]
@Html.EnumDropDownListFor(x => x.AnotherDay, "Select an item")
[/sourcecode]
Using [Description] attribute
If you don't like the generated text, you can customize it using DescriptionAttribute:
[sourcecode language="csharp"]
public enum WeekDay
{
[Description("Domingo")]
Sunday,
[Description("Segunda")]
Monday,
[Description("Terça")]
Tuesday,
[Description("Quarta")]
Wednesday,
[Description("Quinta")]
Thursday,
[Description("Sexta")]
Friday,
[Description("Sábado")]
Saturday
}
[/sourcecode]
Using custom HTML attributes
Just like SelectExtensions.DropDownList Method and SelectExtensions.DropDownListFor Method , you can use custom HTML attributes
[sourcecode language="html"]
@(Html.EnumDropDownList("eventDay", "Select an item", new { @class="select"}))
@(Html.EnumDropDownListFor(x => x.Day, "Select an item" , new { @class="select"}))
[/sourcecode]
More examples - Localization support
The resource file keys for the enum values have the following naming convention: {EnumName}.{EnumValue}
Considering the following resource - type MyResources:
Using [LocalizationEnum] attribute
Just set the [LocalizationEnum] attribute to the enum, specifying the type of the resource object:
[sourcecode language="csharp"]
[LocalizationEnum(typeof(MyResources))]
public enum WeekDay
{
Sunday,
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday
}
[/sourcecode]
The usage is the same as the previous examples:
[sourcecode language="html"]
@Html.EnumDropDownList("selectDay", "")
[/sourcecode]
Specifying the resource object type
If you can't or don't want to use the [LocalizationEnum] attribute, you can specify the resource type using the following helpers:
[sourcecode language="html"]
@(Html.EnumDropDownList("selectDay", ""))
@(Html.EnumDropDownListFor(x => x.Day, "", new {id = "myEventDay"}))
[/sourcecode]
References
ASP.NET MVC - Creating a DropDownList helper for enums
[MSDN] SelectExtensions Class
Downloads
Download the demo project: MusicStore-Resources.rar