Click here to Skip to main content
15,885,914 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.4K   1.4K   29  
A collection of HTML helpers that generate DropDownLists for enums, with or without localization support.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Text;
using System.Web.Mvc;
using System.Web.Mvc.Html;

using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using System.Resources;

using MvcEnumDropDownList.Infrastructure;

namespace MvcEnumDropDownList.Helpers
{
	/// <summary>
	/// ASP.NET MVC - Creating a DropDownList helper for enums
	/// </summary>
	/// <see cref="http://blogs.msdn.com/b/stuartleeks/archive/2010/05/21/asp-net-mvc-creating-a-dropdownlist-helper-for-enums.aspx"/>
	public static partial class DropDownList
	{
		public static MvcHtmlString EnumDropDownList<TEnum>(this HtmlHelper htmlHelper, string name)
		{
			return EnumDropDownList<TEnum>(htmlHelper, name, null /* optionLabel */, null /* htmlAttributes */);
		}

		public static MvcHtmlString EnumDropDownList<TEnum>(this HtmlHelper htmlHelper, string name, string optionLabel)
		{
			return EnumDropDownList<TEnum>(htmlHelper, name, optionLabel, null /* htmlAttributes */);
		}

		public static MvcHtmlString EnumDropDownList<TEnum>(this HtmlHelper htmlHelper, string name, IEnumerable<SelectListItem> selectList)
		{
			return EnumDropDownList<TEnum>(htmlHelper, name, null /* optionLabel */, null /* htmlAttributes */);
		}

		public static MvcHtmlString EnumDropDownList<TEnum>(this HtmlHelper htmlHelper, string name, object htmlAttributes)
		{
			return EnumDropDownList<TEnum>(htmlHelper, name, null /* optionLabel */, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
		}

		public static MvcHtmlString EnumDropDownList<TEnum>(this HtmlHelper htmlHelper, string name, IDictionary<string, object> htmlAttributes)
		{
			return EnumDropDownList<TEnum>(htmlHelper, name, null /* optionLabel */, htmlAttributes);
		}

		public static MvcHtmlString EnumDropDownList<TEnum>(this HtmlHelper htmlHelper, string name, string optionLabel, object htmlAttributes)
		{
			return EnumDropDownList<TEnum>(htmlHelper, name, optionLabel, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
		}

		public static MvcHtmlString EnumDropDownList<TEnum>(this HtmlHelper htmlHelper, string name, string optionLabel, IDictionary<string, object> htmlAttributes)
		{
			IEnumerable<SelectListItem> selectList = GetSelectListItems<TEnum>(null);
			return htmlHelper.DropDownList(name, selectList, optionLabel, htmlAttributes);
		}



		//
		//
		// Specifying Resource type
		//
		//

		public static MvcHtmlString EnumDropDownList<TEnum, TResource>(this HtmlHelper htmlHelper, string name)
		{
			return EnumDropDownList<TEnum, TResource>(htmlHelper, name, null /* optionLabel */, null /* htmlAttributes */);
		}

		public static MvcHtmlString EnumDropDownList<TEnum, TResource>(this HtmlHelper htmlHelper, string name, string optionLabel)
		{
			return EnumDropDownList<TEnum, TResource>(htmlHelper, name, optionLabel, null /* htmlAttributes */);
		}

		public static MvcHtmlString EnumDropDownList<TEnum, TResource>(this HtmlHelper htmlHelper, string name, object htmlAttributes)
		{
			return EnumDropDownList<TEnum, TResource>(htmlHelper, name, null /* optionLabel */, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
		}

		public static MvcHtmlString EnumDropDownList<TEnum, TResource>(this HtmlHelper htmlHelper, string name, IDictionary<string, object> htmlAttributes)
		{
			return EnumDropDownList<TEnum, TResource>(htmlHelper, name, null /* optionLabel */, htmlAttributes);
		}

		public static MvcHtmlString EnumDropDownList<TEnum, TResource>(this HtmlHelper htmlHelper, string name, string optionLabel, object htmlAttributes)
		{
			return EnumDropDownList<TEnum, TResource>(htmlHelper, name, optionLabel, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
		}

		public static MvcHtmlString EnumDropDownList<TEnum, TResource>(this HtmlHelper htmlHelper, string name, string optionLabel, IDictionary<string, object> htmlAttributes)
		{
			IEnumerable<SelectListItem> selectList = GetSelectListItems<TEnum>(typeof(TResource));
			return htmlHelper.DropDownList(name, selectList, optionLabel, htmlAttributes);
		}

	

		#region Private methods 

		/// <summary>
		/// Gets the select list items.
		/// </summary>
		/// <typeparam name="TEnum">The type of the enum.</typeparam>
		/// <param name="selectList">The select list.</param>
		/// <param name="resourceType">Type of the resource.</param>
		/// <returns></returns>
		private static IEnumerable<SelectListItem> GetSelectListItems<TEnum>(Type resourceType)
		{
			bool isEnum = EnumHelper.IsEnum<TEnum>();

			if(!isEnum)
				throw new ArgumentException("TEnum must be an enumerated type");

			IEnumerable<SelectListItem> values = EnumHelper.GetEnumValues<TEnum>(resourceType).Select(x =>
				new SelectListItem {
					Text = x.Value,
					Value = Convert.ToInt32(x.Key).ToString()
				}
			);

			return values;
		}

		private static Type GetNonNullableModelType(ModelMetadata modelMetadata)
		{
			Type realModelType = modelMetadata.ModelType;

			Type underlyingType = Nullable.GetUnderlyingType(realModelType);
			if(underlyingType != null)
			{
				realModelType = underlyingType;
			}
			return realModelType;
		}

		#endregion
	}
}

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