Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I know I'm late to the party on this, but thought you might find this variant useful, as this one also allows you to use descriptive strings rather than enumeration constants in the drop down. To do this, decorate each enumeration entry with a [System.ComponentModel.Description] attribute.</p> <p>For example:</p> <pre><code>public enum TestEnum { [Description("Full test")] FullTest, [Description("Incomplete or partial test")] PartialTest, [Description("No test performed")] None } </code></pre> <p>Here is my code:</p> <pre><code>using System; using System.Collections.Generic; using System.Linq; using System.Web.Mvc; using System.Web.Mvc.Html; using System.Reflection; using System.ComponentModel; using System.Linq.Expressions; ... private static Type GetNonNullableModelType(ModelMetadata modelMetadata) { Type realModelType = modelMetadata.ModelType; Type underlyingType = Nullable.GetUnderlyingType(realModelType); if (underlyingType != null) { realModelType = underlyingType; } return realModelType; } private static readonly SelectListItem[] SingleEmptyItem = new[] { new SelectListItem { Text = "", Value = "" } }; public static string GetEnumDescription&lt;TEnum&gt;(TEnum value) { FieldInfo fi = value.GetType().GetField(value.ToString()); DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false); if ((attributes != null) &amp;&amp; (attributes.Length &gt; 0)) return attributes[0].Description; else return value.ToString(); } public static MvcHtmlString EnumDropDownListFor&lt;TModel, TEnum&gt;(this HtmlHelper&lt;TModel&gt; htmlHelper, Expression&lt;Func&lt;TModel, TEnum&gt;&gt; expression) { return EnumDropDownListFor(htmlHelper, expression, null); } public static MvcHtmlString EnumDropDownListFor&lt;TModel, TEnum&gt;(this HtmlHelper&lt;TModel&gt; htmlHelper, Expression&lt;Func&lt;TModel, TEnum&gt;&gt; expression, object htmlAttributes) { ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData); Type enumType = GetNonNullableModelType(metadata); IEnumerable&lt;TEnum&gt; values = Enum.GetValues(enumType).Cast&lt;TEnum&gt;(); IEnumerable&lt;SelectListItem&gt; items = from value in values select new SelectListItem { Text = GetEnumDescription(value), Value = value.ToString(), Selected = value.Equals(metadata.Model) }; // If the enum is nullable, add an 'empty' item to the collection if (metadata.IsNullableValueType) items = SingleEmptyItem.Concat(items); return htmlHelper.DropDownListFor(expression, items, htmlAttributes); } </code></pre> <p>You can then do this in your view:</p> <pre><code>@Html.EnumDropDownListFor(model =&gt; model.MyEnumProperty) </code></pre> <p>Hope this helps you!</p> <p><strong>EDIT 2014-JAN-23: Microsoft have just released MVC 5.1, which now has an EnumDropDownListFor feature. Sadly it does not appear to respect the [Description] attribute so the code above still stands. (See <a href="http://www.asp.net/mvc/overview/releases/mvc51-release-notes#Enum" rel="noreferrer">http://www.asp.net/mvc/overview/releases/mvc51-release-notes#Enum</a></strong> for Microsoft's release notes.) </p> <p><strong>Update: It does support the <a href="https://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.displayattribute%28v=vs.110%29.aspx" rel="noreferrer">Display</a> attribute <code>[Display(Name = "Sample")]</code> though, so one can use that.</strong></p> <p>[Update - just noticed this, and the code looks like an extended version of the code here: <a href="http://blogs.msdn.com/b/stuartleeks/archive/2010/05/21/asp-net-mvc-creating-a-dropdownlist-helper-for-enums.aspx" rel="noreferrer">http://blogs.msdn.com/b/stuartleeks/archive/2010/05/21/asp-net-mvc-creating-a-dropdownlist-helper-for-enums.aspx</a>, with a couple of additions. If so, attribution would seem fair ;-)]</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload