Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to make a default editor template for enums?
    primarykey
    data
    text
    <p>How can I make a default editor template for enums? By which I mean: can I do something like this:</p> <pre><code>&lt;%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl&lt;Enum&gt;" %&gt; &lt;% -- any code to read the enum and write a dropdown --&gt; </code></pre> <p>And put this in the EditorTemplates folder under the name <code>Enum.ascx</code>?</p> <p>Here's a workaround for my problem that I tried, but it's not what I need.</p> <p>Here is my Enum:</p> <pre><code>public enum GenderEnum { /// &lt;summary&gt; /// Male /// &lt;/summary&gt; [Description("Male Person")] Male, /// &lt;summary&gt; /// Female /// &lt;/summary&gt; [Description("Female Person")] Female } </code></pre> <p>I made a template called <code>GenderEnum.acsx</code> and put it in the <code>Shared/EditorTemplates</code> folder. Here is the Template:</p> <pre><code>&lt;%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl&lt;AlefTech.HumanResource.Core.GenderEnum&gt;" %&gt; &lt;%@ Import Namespace="AlefTech.HumanResource.WebModule.Classes" %&gt; &lt;%=Html.DropDownListFor(m =&gt; m.GetType().Name, Model.GetType()) %&gt; </code></pre> <p>Of course the method is my own: </p> <pre><code>public static class HtmlHelperExtension { public static MvcHtmlString DropDownListFor&lt;TModel, TProperty&gt;(this HtmlHelper&lt;TModel&gt; htmlHelper, Expression&lt;Func&lt;TModel, TProperty&gt;&gt; expression, Type enumType) { List&lt;SelectListItem&gt; list = new List&lt;SelectListItem&gt;(); Dictionary&lt;string, string&gt; enumItems = enumType.GetDescription(); foreach (KeyValuePair&lt;string, string&gt; pair in enumItems) list.Add(new SelectListItem() { Value = pair.Key, Text = pair.Value }); return htmlHelper.DropDownListFor(expression, list); } /// &lt;summary&gt; /// return the items of enum paired with its descrtioption. /// &lt;/summary&gt; /// &lt;param name="enumeration"&gt;enumeration type to be processed.&lt;/param&gt; /// &lt;returns&gt;&lt;/returns&gt; public static Dictionary&lt;string, string&gt; GetDescription(this Type enumeration) { if (!enumeration.IsEnum) { throw new ArgumentException("passed type must be of Enum type", "enumerationValue"); } Dictionary&lt;string, string&gt; descriptions = new Dictionary&lt;string, string&gt;(); var members = enumeration.GetMembers().Where(m =&gt; m.MemberType == MemberTypes.Field); foreach (MemberInfo member in members) { var attrs = member.GetCustomAttributes(typeof(DescriptionAttribute), false); if (attrs.Count() != 0) descriptions.Add(member.Name, ((DescriptionAttribute)attrs[0]).Description); } return descriptions; } } </code></pre> <p>However, even though this worked for me, it is not what I'm asking. Instead, I need the following to work:</p> <p>Code for <code>Shared\EditorTemplates\Enum.acsx</code>:</p> <pre><code>&lt;%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl&lt;Enum&gt;" %&gt; &lt;%@ Import Namespace="System.Web.Mvc.Html" %&gt; &lt;%@ Import Namespace="WhereMyExtentionMethod" %&gt; &lt;%=Html.DropDownListFor(m =&gt; m.GetType().Name, Model.GetType()) %&gt; </code></pre> <p>With this I wouldn't have to make a template for every enum any more.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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