Click here to Skip to main content
Click here to Skip to main content

ASP.NET MVC: Creating localized DropDownLists for enums

By , 6 Nov 2012
Rate this:
Please Sign up or sign in to vote.

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Rui Jarimba
Software Developer (Senior) Dublin Entrprise & Technology Centre Limited
Ireland Ireland
My name is Rui Jarimba and I was born in Madeira island, Portugal and I currently live in Dublin, Ireland.
 
I’m working as a .NET software developer since 2005.
 
Some of my professional interests are:
 
Web development using .NET Framework;
Service Oriented Architecture (SOA);
Database development and modelling;
Web accessibility, usability, and standards;
Software Architecture;
Design Patterns
Follow on   Twitter   LinkedIn

Comments and Discussions

 
GeneralMy vote of 5 Pinmemberenriquecp123-Aug-13 13:03 
GeneralRe: My vote of 5 PinmemberRui Jarimba27-Aug-13 0:17 
GeneralMy vote of 5 PinmemberSavalia Manoj M7-Nov-12 1:09 
GeneralRe: My vote of 5 PinmemberRui Jarimba7-Nov-12 1:10 
GeneralMy vote of 5 PinmemberАslam Iqbal6-Nov-12 23:18 
GeneralRe: My vote of 5 PinmemberRui Jarimba6-Nov-12 23:27 
GeneralMy vote of 2 Pinmemberjeffb4217-Feb-12 6:09 
GeneralRe: My vote of 2 PinmemberRui Jarimba17-Feb-12 6:12 
GeneralRe: My vote of 2 PinmemberRui Jarimba17-Feb-12 6:25 
GeneralRe: My vote of 2 PinmemberRui Jarimba17-Feb-12 6:37 
GeneralRe: My vote of 2 [modified] Pinmemberjeffb4217-Feb-12 11:07 
GeneralRe: My vote of 2 PinmemberRui Jarimba17-Feb-12 15:21 
GeneralRe: My vote of 2 PinmemberRui Jarimba20-Feb-12 9:37 
GeneralRe: My vote of 2 Pinmemberjeffb4229-Feb-12 11:05 
GeneralRe: My vote of 2 PinmemberRui Jarimba29-Feb-12 11:47 
GeneralRe: My vote of 2 PinmemberRui Jarimba2-Mar-12 8:48 
GeneralRe: My vote of 2 PinmemberАslam Iqbal6-Nov-12 23:18 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.140415.2 | Last Updated 6 Nov 2012
Article Copyright 2012 by Rui Jarimba
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid