Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

ASP.NET MVC: Creating localized DropDownLists for enums

0.00/5 (No votes)
6 Nov 2012 1  
A collection of HTML helpers that generate DropDownLists for enums, with or without localization support.

Introduction

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 the DropDownList method and DropDownListFor method, with the only difference being that those helpers will populate the DropDownList with the elements of the specified enum.

Some examples – Basic usage

Let’s assume the following model:

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; }
}

Setting the name of the element and default empty item text

This is the most basic usage, the generated text will be enumValue.ToString():

// name="eventDay"
@Html.EnumDropDownList<WeekDay>("eventDay", "Select an item") 

// name="Day"
@Html.EnumDropDownListFor<WeeklyEvent, WeekDay>(x => x.Day, "Select an item") 

Html.EnumDropDownListFor works with nullables too:

@Html.EnumDropDownListFor<WeeklyEvent, WeekDay?>(x => x.AnotherDay, "Select an item") 

Using the [Description] attribute

You can customize the text using DescriptionAttribute:

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
}

Using custom HTML attributes

Just like DropDownList and DropDownListFor, you can use custom HTML attributes.

@Html.EnumDropDownList<WeekDay>("eventDay", "Select an item", new { @class="select"})
@Html.EnumDropDownListFor<WeeklyEvent, WeekDay>(x => x.Day, "Select an item" , new { @class="select"})

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 the [LocalizationEnum] attribute

Just set the [LocalizationEnum] attribute to the enum, specifying the type of the resource object:

[LocalizationEnum(typeof(MyResources))]
public enum WeekDay
{
    Sunday,
    Monday,
    Tuesday,
    Wednesday,
    Thursday,
    Friday,
    Saturday
}

The usage is the same as the previous examples:

@Html.EnumDropDownList<WeekDay>("selectDay", "")

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:

@Html.EnumDropDownList<WeekDay, MyResources>("selectDay", "")
@Html.EnumDropDownListFor<WeeklyEvent, WeekDay, MyResources>(x => x.Day, "", new {id = "myEventDay"})

References

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here