Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Nour Sabony, I modified your version to also support localization with resources. Therefore I changed the DescriptionAttribute to the DisplayAttribute of the DataAnnotations namespace</p> <pre><code> 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.GetDisplayNames(htmlHelper.ViewContext.HttpContext); 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 DisplayName. /// &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; GetDisplayNames(this Type enumeration, HttpContextBase httpContext) { if (!enumeration.IsEnum) { throw new ArgumentException("passed type must be of Enum type", "enumerationValue"); } Dictionary&lt;string, string&gt; displayNames = 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(DisplayAttribute), false); if (attrs.Count() != 0) if (((DisplayAttribute)attrs[0]).ResourceType != null) { displayNames.Add(member.Name, ((DisplayAttribute)attrs[0]).GetName();); } else { displayNames.Add(member.Name, ((DisplayAttribute)attrs[0]).Name); } } return displayNames; } </code></pre> <p>The definition of an enum has to look like this now:</p> <pre><code>public enum Gender { [Display(Name = "Male", ResourceType = typeof(mynamespace.App_LocalResources.Shared))] Male = 1, [Display(Name = "Female", ResourceType = typeof(mynamespace.App_LocalResources.Shared))] Female = 2, } </code></pre> <p>it can be used in a View in the same way, e.g. (Razor):</p> <pre><code>@Html.DropDownListFor(model =&gt; model.Gender, typeof(Gender)) </code></pre> <p>Hope this helps someone!</p>
    singulars
    1. This table or related slice is empty.
    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.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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