Enum DropDownList in ASP.NET MVC
I created an HTML Helper for rendering a dropdownlist aka select element from the name/value pairs in an enum and thought I would share.
It's fairly simple to use: add the source code file to your project and recompile. You should then have two additional Html.Helper overridden methods for DropDownList.
Usage:
Html.DropDownList(string Name, Type enumtype)
Html.DropDownList(string Name, Type enumtype, object selectedEnum)
Html.DropDownList(string Name, Type enumtype, object selectedEnum)
Examples of usage:
<%=Html.DropDownList("Status", typeof(StatusEnum), Model.Status) %>
Finally, the source, also available for download. Note class uses extension methods and is static with static methods.
using System.Collections.Generic;
namespace System.Web.Mvc
{
public static class EnumDropDownList
{
/// <summary>
/// Creates a dropdown list from an enum
/// </summary>
/// <param name="name">name of the element</param>
/// <param name="type">the enum type</param>
/// <returns></returns>
public static string DropDownList(this HtmlHelper helper, string name, Type type)
{
return DropDownList(helper, name, type, null);
}
/// <summary>
/// Creates a dropdown list from an enum.
/// </summary>
/// <param name="name">name of an element</param>
/// <param name="type">the enum type</param>
/// <param name="selected">selected enum value</param>
/// <returns></returns>
public static string DropDownList(this HtmlHelper helper, string name, Type type, object selected)
{
if (!type.IsEnum)
throw new ArgumentException("Type is not an enum.");
if(selected != null && selected.GetType() != type)
throw new ArgumentException("Selected object is not " + type.ToString());
var enums = new List<SelectListItem>();
foreach (int value in Enum.GetValues(type))
{
var item = new SelectListItem();
item.Value = value.ToString();
item.Text = Enum.GetName(type, value);
if(selected != null)
item.Selected = (int)selected == value;
enums.Add(item);
}
return System.Web.Mvc.Html.SelectExtensions.DropDownList(helper, name, enums);
}
}
}
namespace System.Web.Mvc
{
public static class EnumDropDownList
{
/// <summary>
/// Creates a dropdown list from an enum
/// </summary>
/// <param name="name">name of the element</param>
/// <param name="type">the enum type</param>
/// <returns></returns>
public static string DropDownList(this HtmlHelper helper, string name, Type type)
{
return DropDownList(helper, name, type, null);
}
/// <summary>
/// Creates a dropdown list from an enum.
/// </summary>
/// <param name="name">name of an element</param>
/// <param name="type">the enum type</param>
/// <param name="selected">selected enum value</param>
/// <returns></returns>
public static string DropDownList(this HtmlHelper helper, string name, Type type, object selected)
{
if (!type.IsEnum)
throw new ArgumentException("Type is not an enum.");
if(selected != null && selected.GetType() != type)
throw new ArgumentException("Selected object is not " + type.ToString());
var enums = new List<SelectListItem>();
foreach (int value in Enum.GetValues(type))
{
var item = new SelectListItem();
item.Value = value.ToString();
item.Text = Enum.GetName(type, value);
if(selected != null)
item.Selected = (int)selected == value;
enums.Add(item);
}
return System.Web.Mvc.Html.SelectExtensions.DropDownList(helper, name, enums);
}
}
}
| Attachment | Size |
|---|---|
| EnumDropDownList.cs | 1.75 KB |
Recent blog posts
- Canned VirtualPC Instances for IE 6, 7, 8 on XP/Vista
- Checking assembly dependencies for .NET
- Google's Public DNS
- Server Utility Functions for Non-Web Apps
- reCAPTCHA for ASP.NET MVC that uses ModelState
- Adding a container to ValidationSummary helper in ASP.NET MVC
- Generic XML Serialization Class
- Re-throwing Exceptions in C# with InternalPreserveStackTrace
- Solving xsd generation error: 'The element .... is missing'
- Enum DropDownList in ASP.NET MVC
Recent comments
- so very helpful kenny. thank
9 weeks 2 days ago - Sorry, apparently the drupal
12 weeks 3 days ago - A better method
15 weeks 4 days ago - No Source Code?
21 weeks 2 days ago - security
31 weeks 1 day ago - Nice simple solution
1 year 15 weeks ago - That's quite an interesting
1 year 32 weeks ago - Small Complaint
1 year 33 weeks ago - NIceeee
1 year 39 weeks ago - Both the SQL Server connector
2 years 16 weeks ago
Comments
#1 Anonymous
I ran into a bit of a funny issue with this. Ended up needing to set the Text AND Value of the select list items to the value you are currently setting for text.
See this StackOverflow post for more info: http://stackoverflow.com/questions/3032743/selected-item-in-dropdown-lis...
#2 khuang
That's quite an interesting bit of detail there. Sadly I'm not the one using MVC (that's Brian's territory) so I can't confirm but I think the link to the StackOverflow issue is more than sufficient for the time being.