Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is a better encapsulated solution:</p> <p><a href="http://www.spicelogic.com/Journal/ASP-NET-MVC-DropDownListFor-Html-Helper-Enum-5" rel="noreferrer">http://www.spicelogic.com/Journal/ASP-NET-MVC-DropDownListFor-Html-Helper-Enum-5</a></p> <p>Say here is your model:</p> <p><img src="https://i.stack.imgur.com/LhPGx.png" alt="enter image description here"></p> <p><strong>Sample Usage:</strong></p> <p><img src="https://i.stack.imgur.com/4YeEG.png" alt="enter image description here"></p> <p><strong>Generated UI:</strong> <img src="https://i.stack.imgur.com/3eEC0.png" alt="enter image description here"></p> <p><strong>And generated HTML</strong></p> <p><img src="https://i.stack.imgur.com/QESvd.png" alt="enter image description here"></p> <p><strong>The Helper Extension Source Code snap shot:</strong></p> <p><img src="https://i.stack.imgur.com/m3loZ.png" alt="enter image description here"></p> <p>You can download the sample project from the link I provided.</p> <p>EDIT: Here's the code:</p> <pre><code>public static class EnumEditorHtmlHelper { /// &lt;summary&gt; /// Creates the DropDown List (HTML Select Element) from LINQ /// Expression where the expression returns an Enum type. /// &lt;/summary&gt; /// &lt;typeparam name="TModel"&gt;The type of the model.&lt;/typeparam&gt; /// &lt;typeparam name="TProperty"&gt;The type of the property.&lt;/typeparam&gt; /// &lt;param name="htmlHelper"&gt;The HTML helper.&lt;/param&gt; /// &lt;param name="expression"&gt;The expression.&lt;/param&gt; /// &lt;returns&gt;&lt;/returns&gt; public static MvcHtmlString DropDownListFor&lt;TModel, TProperty&gt;(this HtmlHelper&lt;TModel&gt; htmlHelper, Expression&lt;Func&lt;TModel, TProperty&gt;&gt; expression) where TModel : class { TProperty value = htmlHelper.ViewData.Model == null ? default(TProperty) : expression.Compile()(htmlHelper.ViewData.Model); string selected = value == null ? String.Empty : value.ToString(); return htmlHelper.DropDownListFor(expression, createSelectList(expression.ReturnType, selected)); } /// &lt;summary&gt; /// Creates the select list. /// &lt;/summary&gt; /// &lt;param name="enumType"&gt;Type of the enum.&lt;/param&gt; /// &lt;param name="selectedItem"&gt;The selected item.&lt;/param&gt; /// &lt;returns&gt;&lt;/returns&gt; private static IEnumerable&lt;SelectListItem&gt; createSelectList(Type enumType, string selectedItem) { return (from object item in Enum.GetValues(enumType) let fi = enumType.GetField(item.ToString()) let attribute = fi.GetCustomAttributes(typeof (DescriptionAttribute), true).FirstOrDefault() let title = attribute == null ? item.ToString() : ((DescriptionAttribute) attribute).Description select new SelectListItem { Value = item.ToString(), Text = title, Selected = selectedItem == item.ToString() }).ToList(); } } </code></pre>
    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.
    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.
 

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