Click here to Skip to main content
15,886,110 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.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Globalization;
using MvcEnumDropDownList.Infrastructure;
using System.Threading;

namespace MvcEnumDropDownList
{
	// Note: For instructions on enabling IIS6 or IIS7 classic mode, 
	// visit http://go.microsoft.com/?LinkId=9394801

	public class MvcApplication : System.Web.HttpApplication
	{
		public static void RegisterGlobalFilters(GlobalFilterCollection filters)
		{
			filters.Add(new HandleErrorAttribute());
		}

		public static void RegisterRoutes(RouteCollection routes)
		{
			routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

			routes.MapRoute(
				"Default", // Route name
				"{controller}/{action}/{id}", // URL with parameters
				new
				{
					controller = "Home",
					action = "Index",
					id = UrlParameter.Optional
				} // Parameter defaults
			);

		}

		protected void Application_Start()
		{
			AreaRegistration.RegisterAllAreas();

			RegisterGlobalFilters(GlobalFilters.Filters);
			RegisterRoutes(RouteTable.Routes);
		}

		protected void Application_AcquireRequestState(object sender, EventArgs e)
		{
			//It's important to check whether session object is ready
			if(HttpContext.Current.Session == null)
				return;

			CultureInfo ci = SessionHelper.Culture;

			//Checking first if there is no value in session 
			//and set default language 
			//this can happen for first user's request
			if(ci == null)
			{
				//Sets default culture to portuguese invariant
				string langName = "pt";

				//Try to get values from Accept lang HTTP header
				if(HttpContext.Current.Request.UserLanguages != null && HttpContext.Current.Request.UserLanguages.Length != 0)
				{
					//Gets accepted list 
					langName = HttpContext.Current.Request.UserLanguages[0].Substring(0, 2);
				}

				ci = new CultureInfo(langName);
				SessionHelper.Culture = ci;
			}

			//Finally setting culture for each request
			Thread.CurrentThread.CurrentUICulture = ci;
			Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(ci.Name);
		}
	}
}

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