Click here to Skip to main content
14,982,989 members
Articles / Web Development / HTML
Article
Posted 17 Feb 2012

Stats

52K views
1.4K downloads
29 bookmarked

ASP.NET MVC: Creating localized DropDownLists for enums

Rate me:
Please Sign up or sign in to vote.
4.80/5 (12 votes)
6 Nov 2012CPOL1 min read
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:

C#
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():

C#
// 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:

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

Using the [Description] attribute

You can customize the text using DescriptionAttribute:

C#
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.

C#
@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:

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

The usage is the same as the previous examples:

C#
@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:

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

References

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Rui Jarimba
Software Developer (Senior)
Italy Italy
My name is Rui Jarimba and I was born in Madeira island, Portugal and I currently live in Rome, Italy.

I have more than 10 years of experience developing software using the .NET Framework and other technologies (Web development, Databases, ...).

Some of my professional interests are: software development best practices, software architecture, cloud computing, Continuous Integration (CI), Continuous Delivery (CD) and agile methodologies such as Scrum, Kanban, Lean and any other methodology that can help me to become a better and more productive software engineer.

I believe in good code - code that is readable, maintainable, reusable, testable and deployable. This means that I'm not the "quick and dirty" type, I write code for the medium/long term whenever possible.

Something else about me - I love music, I am an amateur photographer, not a big fan of gyms (I prefer to do some outdoor activity such as walking/hiking), big foodie (I love Mediterranean cuisine and my glass of wine!).

Comments and Discussions

 
QuestionError Pin
jayz0939-Sep-14 7:28
Memberjayz0939-Sep-14 7:28 
GeneralMy vote of 5 Pin
Enrique Carro23-Aug-13 13:03
MemberEnrique Carro23-Aug-13 13:03 
GeneralRe: My vote of 5 Pin
Rui Jarimba27-Aug-13 0:17
professionalRui Jarimba27-Aug-13 0:17 
GeneralMy vote of 5 Pin
Savalia Manoj M7-Nov-12 1:09
MemberSavalia Manoj M7-Nov-12 1:09 
GeneralRe: My vote of 5 Pin
Rui Jarimba7-Nov-12 1:10
professionalRui Jarimba7-Nov-12 1:10 
GeneralMy vote of 5 Pin
Аslam Iqbal6-Nov-12 23:18
professionalАslam Iqbal6-Nov-12 23:18 
GeneralRe: My vote of 5 Pin
Rui Jarimba6-Nov-12 23:27
professionalRui Jarimba6-Nov-12 23:27 
GeneralMy vote of 2 Pin
jeffb4217-Feb-12 6:09
Memberjeffb4217-Feb-12 6:09 
GeneralRe: My vote of 2 Pin
Rui Jarimba17-Feb-12 6:12
professionalRui Jarimba17-Feb-12 6:12 
GeneralRe: My vote of 2 Pin
Rui Jarimba17-Feb-12 6:25
professionalRui Jarimba17-Feb-12 6:25 
GeneralRe: My vote of 2 Pin
Rui Jarimba17-Feb-12 6:37
professionalRui Jarimba17-Feb-12 6:37 
GeneralRe: My vote of 2 Pin
jeffb4217-Feb-12 11:07
Memberjeffb4217-Feb-12 11:07 
GeneralRe: My vote of 2 Pin
Rui Jarimba17-Feb-12 15:21
professionalRui Jarimba17-Feb-12 15:21 
GeneralRe: My vote of 2 Pin
Rui Jarimba20-Feb-12 9:37
professionalRui Jarimba20-Feb-12 9:37 
GeneralRe: My vote of 2 Pin
jeffb4229-Feb-12 11:05
Memberjeffb4229-Feb-12 11:05 
GeneralRe: My vote of 2 Pin
Rui Jarimba29-Feb-12 11:47
professionalRui Jarimba29-Feb-12 11:47 
GeneralRe: My vote of 2 Pin
Rui Jarimba2-Mar-12 8:48
professionalRui Jarimba2-Mar-12 8:48 
GeneralRe: My vote of 2 Pin
Аslam Iqbal6-Nov-12 23:18
professionalАslam Iqbal6-Nov-12 23:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.