Click here to Skip to main content
15,886,199 members
Articles / Programming Languages / C#
Tip/Trick

MVC - Rendering DropDownList of Enumerators

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
18 Nov 2017CPOL1 min read 8.6K   3   8
One less pesky nuance to worry about

Introduction

We've all been there - how do I put enumerators into a drop down on a mvc web site? This is how I did it.

The Code

Given the following enumerator defintion:

C#
public enum LocationType { Zipcode=0, City, County, State, Country, Address};

I created the following code. I started off with a base class, allowing me to continue with my chosen technique with additional enumerators:

C#
public abstract class EnumListItem
{
    public string Key   { get; set; }
    public int    Value { get; set; }
}

And then inherited my item class from it:

C#
public class LocTypeItem : EnumListItem
{
    public LocationType LocType 
    { 
        get { return Globals.IntToEnum(this.Value, LocationType.City) ; }
        set { this.Value = (int)value; }
    }
}

Next, I create a list class, and initialized it in the constructor:

C#
public class LocationTypeList : List<LocTypeItem>
{
    public LocationTypeList()
    {
        this.Add(new LocTypeItem() { Key = "City",        LocType = LocationType.City    });
        this.Add(new LocTypeItem() { Key = "County",      LocType = LocationType.County  });
        this.Add(new LocTypeItem() { Key = "Country",     LocType = LocationType.Country });
        this.Add(new LocTypeItem() { Key = "State",       LocType = LocationType.State   });
        this.Add(new LocTypeItem() { Key = "Postal Code", LocType = LocationType.Zipcode });	
    }
}

And finally, I created a static Globals class with a property for the list so I could access it anytime I want, from anywhere I want:

C#
public static partial class Globals
{
    public static LocationTypeList ListOfLocationTypes { get; set; }

    static Globals()
    {
        ListOfLocationTypes = new LocationTypeList();
    }
    
   // Used in the classes above to translate ints to the appropriate enum type
    public static T IntToEnum<T>(int value, T defaultValue)
	{
        T enumValue = (Enum.IsDefined(typeof(T), value)) ? (T)(object)value : defaultValue;
        return enumValue;
    }
}    

Using the code

In the view that renders the dropdownlist control, it looks like this:

HTML
@Html.DropDownListForEx(model => model.MyIntVar, new SelectList(ViewBag.LocationTypes,"Value","Key"))

Note - The DropDownListForEx method is an extension method that takes car of wrapping the dropdownlist in bootsrap styles and divs. As you can see it really de-clutters the code in your view. One of these days, I'll write an article about these extension methods (I have one for pretty much every standard control you'd see in a MVC app.

Points of Interest

I like to bury complexity as much as is reasonable, so the stuff I need to do all the time is easier.

History

22 Nov 2017 - Forgot to paste the Globals.IntToEnum method in the right place.

21 Nov 2017 - Forgot to include the Globals.IntToEnum method.

18 Nov 2017 - Inital post.

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) Paddedwall Software
United States United States
I've been paid as a programmer since 1982 with experience in Pascal, and C++ (both self-taught), and began writing Windows programs in 1991 using Visual C++ and MFC. In the 2nd half of 2007, I started writing C# Windows Forms and ASP.Net applications, and have since done WPF, Silverlight, WCF, web services, and Windows services.

My weakest point is that my moments of clarity are too brief to hold a meaningful conversation that requires more than 30 seconds to complete. Thankfully, grunts of agreement are all that is required to conduct most discussions without committing to any particular belief system.

Comments and Discussions

 
BugRendering DDL of Enum Pin
heymikej21-Nov-17 3:20
heymikej21-Nov-17 3:20 
GeneralRe: Rendering DDL of Enum Pin
#realJSOP22-Nov-17 1:15
mve#realJSOP22-Nov-17 1:15 
QuestionIntToEnum Pin
Łukasz Tomalczyk20-Nov-17 6:30
Łukasz Tomalczyk20-Nov-17 6:30 
AnswerRe: IntToEnum Pin
#realJSOP21-Nov-17 1:29
mve#realJSOP21-Nov-17 1:29 
GeneralRe: IntToEnum Pin
Łukasz Tomalczyk21-Nov-17 4:44
Łukasz Tomalczyk21-Nov-17 4:44 
GeneralRe: IntToEnum Pin
#realJSOP22-Nov-17 1:10
mve#realJSOP22-Nov-17 1:10 
GeneralRe: IntToEnum Pin
Łukasz Tomalczyk22-Nov-17 6:04
Łukasz Tomalczyk22-Nov-17 6:04 
GeneralRe: IntToEnum Pin
#realJSOP23-Nov-17 3:00
mve#realJSOP23-Nov-17 3:00 

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.