Click here to Skip to main content
15,896,286 members
Articles / Web Development / HTML

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 59.7K   1.4K   29  
A collection of HTML helpers that generate DropDownLists for enums, with or without localization support.
A collection of HTML helpers that generate DropDownlists for enums, with or without localization support.

<strong>Table of contents</strong>
<ul>
	<li><a title="HTML Helpers Overview" href="#section0">HTML Helpers Overview</a></li>
	<li><a title="Some examples" href="#section1">Some examples - Basic usage</a></li>
	<ul>
		<li><a href="#section1-1">Setting the name of the element and default empty item text</a></li>
		<li><a href="#section1-2">Using [Description] attribute</a></li>
		<li><a href="#section1-3">Using custom HTML attributes</a></li>
	</ul>
	<li><a title="More examples - Localization support" href="#section2">More examples - Localization support</a></li>
	<ul>
		<li><a href="#section2-1">Using [LocalizationEnum] attribute</a></li>
		<li><a href="#section2-2">Specifying the resource object type</a></li>
	</ul>
	<li><a href="#section-references">References</a></li>
	<li><a href="#section-downloads">Downloads</a></li>
</ul>


<a style="color: #fff; margin-top: 20px;" name="section0"></a>
<h2 style="border-bottom: 1px solid #3366ff; font-size: 14pt;"><span style="color: #3366ff;">HTML Helpers Overview
</span></h2>

I've created a set of HTML helpers that generate a DropDownList for an enum.
Those helpers are similar to <a title="[MSDN] SelectExtensions.DropDownList Method" href="http://msdn.microsoft.com/en-us/library/system.web.mvc.html.selectextensions.dropdownlist.aspx" target="_blank">SelectExtensions.DropDownList Method</a> and 
<a title="[MSDN] SelectExtensions.DropDownListFor Method" href="http://msdn.microsoft.com/en-us/library/system.web.mvc.html.selectextensions.dropdownlist.aspx" target="_blank">SelectExtensions.DropDownListFor Method </a>, 
with the only difference being that the those helpers will populate the DropDownList with the elements of the specified enum.








<a style="color: #fff; margin-top: 20px;" name="section1"></a>
<h2 style="border-bottom: 1px solid #3366ff; font-size: 14pt;"><span style="color: #3366ff;">Some examples - Basic usage</span></h2>
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]


<a style="color: #fff; margin-top: 20px;" name="section1-1"></a>
<h3 style="font-size: 12pt;"><strong>Setting the name of the element and default empty item text</strong></h3>
This is the most basic usage, the generated text will be <strong><i>enumValue</i>.ToString()</strong>:
[sourcecode language="html"]
@Html.EnumDropDownList<WeekDay>("eventDay", "Select an item") // name="eventDay"

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

Html.EnumDropDownListFor works with nullables too:
[sourcecode language="html"]
@Html.EnumDropDownListFor<WeeklyEvent, WeekDay>(x => x.AnotherDay, "Select an item") 
[/sourcecode]



<a style="color: #fff; margin-top: 20px;" name="section1-2"></a>
<h3 style="font-size: 12pt;"><strong>Using [Description] attribute</strong></h3>
If you don't like the generated text, you can customize it using <a title="System.ComponentModel.DescriptionAttribute" href="http://msdn.microsoft.com/en-us/library/system.componentmodel.descriptionattribute.aspx">DescriptionAttribute</a>:

[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]

<a style="color: #fff; margin-top: 20px;" name="section1-3"></a>
<h3 style="font-size: 12pt;"><strong>Using custom HTML attributes</strong></h3>
Just like <a title="[MSDN] SelectExtensions.DropDownList Method" href="http://msdn.microsoft.com/en-us/library/system.web.mvc.html.selectextensions.dropdownlist.aspx" target="_blank">SelectExtensions.DropDownList Method</a> and <a title="[MSDN] SelectExtensions.DropDownListFor Method" href="http://msdn.microsoft.com/en-us/library/system.web.mvc.html.selectextensions.dropdownlist.aspx" target="_blank">SelectExtensions.DropDownListFor Method </a>, you can use custom HTML attributes
[sourcecode language="html"]
@(Html.EnumDropDownList<WeekDay>("eventDay", "Select an item", new { @class="select"})) 

@(Html.EnumDropDownListFor<WeeklyEvent, WeekDay>(x => x.Day, "Select an item" , new { @class="select"}))
[/sourcecode]








<a style="color: #ffffff; margin-top: 40px;" name="section2"></a><!--more-->
<h2 style="border-bottom: 1px solid #3366ff; font-size: 14pt;"><span style="color: #3366ff;">More examples - Localization support</span></h2>
The resource file keys for the enum values have the following naming convention: <strong>{EnumName}.{EnumValue}</strong>
Considering the following resource - type <strong>MyResources</strong>:

<a style="color: #fff; margin-top: 20px;" name="section2-1"></a>
<h3 style="font-size: 12pt;"><strong>Using [LocalizationEnum] attribute</strong></h3>
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<WeekDay>("selectDay", "")
[/sourcecode]



<a style="color: #fff; margin-top: 20px;" name="section2-2"></a>
<h3 style="font-size: 12pt;"><strong>Specifying the resource object type</strong></h3>
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<WeekDay, MyResources>("selectDay", ""))

@(Html.EnumDropDownListFor<WeeklyEvent, WeekDay, MyResources>(x => x.Day, "", new {id = "myEventDay"}))
[/sourcecode]














<a style="color: #ffffff; margin-top: 40px;" name="section-references"></a>
<h2 style="border-bottom: 1px solid #3366ff; font-size: 14pt; margin-top: 20px;"><span style="color: #3366ff;">References</span></h2>
<a title="ASP.NET MVC - Creating a DropDownList helper for enums" href="http://blogs.msdn.com/b/stuartleeks/archive/2010/05/21/asp-net-mvc-creating-a-dropdownlist-helper-for-enums.aspx" target="_blank">ASP.NET MVC - Creating a DropDownList helper for enums</a>

<a title="[MSDN] SelectExtensions Class" href="http://msdn.microsoft.com/en-us/library/system.web.mvc.html.selectextensions.aspx" target="_blank">[MSDN] SelectExtensions Class</a>


<a style="color: #fff; margin-top: 20px;" name="section-downloads"></a>
<h2 style="border-bottom: 1px solid #3366ff; font-size: 14pt;"><span style="color: #3366ff;">Downloads</span></h2>
Download the demo project: <a title="MvcEnumDropDonwList.zip" href="https://docs.google.com/uc?id=0B411XiA1vaSzOWQ0ZTZhMGYtZDZiMS00Y2RkLWJiMGEtMDYyM2YyZGZkNGNj&amp;export=download" target="_blank">MusicStore-Resources.rar</a>
<div id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:5181506a-8050-40d9-a476-7935b9f7f7d9" class="wlWriterEditableSmartContent" style="display: inline; float: none; margin: 0; padding: 0;">Technorati Tags: <a href="http://technorati.com/tags/.NET" rel="tag">.NET</a>, <a href="http://technorati.com/tags/ASP.NET+MVC" rel="tag">ASP.NET MVC</a>, <a href="http://technorati.com/tags/C%23" rel="tag">C#</a>, <a href="http://technorati.com/tags/Globalization" rel="tag">Globalization</a>, <a href="http://technorati.com/tags/Localization" rel="tag">Localization</a>, <a href="http://technorati.com/tags/T4" rel="tag">T4</a></div>
<a href="http://www.dotnetkicks.com/kick/?url=http://ruijarimba.wordpress.com/2011/05/16/asp-net-mvc-localization-generate-resource-files-and-localized-views-using-custom-templates/"><img src="//ruijarimba.wordpress.com/2011/05/16/asp-net-mvc-localization-generate-resource-files-and-localized-views-using-custom-templates/&amp;bgcolor=FFCC00" alt="kick it on DotNetKicks.com" border="0" /></a>
<div class="shoutIt"><a href="http://dotnetshoutout.com/Submit?url=http://ruijarimba.wordpress.com/2011/05/16/asp-net-mvc-localization-generate-resource-files-and-localized-views-using-custom-templates/" rev="vote-for">
<img style="border: 0;" src="http://dotnetshoutout.com/image.axd?url=http://ruijarimba.wordpress.com/2011/05/16/asp-net-mvc-localization-generate-resource-files-and-localized-views-using-custom-templates/" alt="Shout it" />
</a>
<div>

<a style="display: none;" href="http://www.codeproject.com/script/Articles/BlogArticleList.aspx?amid=2431313" rel="tag">CodeProject</a>

</div>
</div>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
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