Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I am still learning, but I had a similar problem for which I worked out a solution. My Category is an enum and I use a template control which examines the enum to determine the contents for the Select tag.</p> <p>It is used in the view as:</p> <pre><code>&lt;%= Html.DropDownList ( "CategoryCode", MvcApplication1.Utility.EditorTemplates.SelectListForEnum(typeof(WebSite.ViewData.Episode.Procedure.Category), selectedItem) ) %&gt; </code></pre> <p>The enum for Category is decorated with Description attributes to be used as the text values in the Select items:</p> <pre><code> public enum Category { [Description("Operative")] Operative=1, [Description("Non Operative")] NonOperative=2, [Description("Therapeutic")] Therapeutic=3 } private Category _CategoryCode; public Category CategoryCode { get { return _CategoryCode; } set { _CategoryCode = value; } } </code></pre> <p>The SelectListForEnum constructs the list of select items using the enum definition and the index for the currently selected item, as follows:</p> <pre><code> public static SelectListItem[] SelectListForEnum(System.Type typeOfEnum, int selectedItem) { var enumValues = typeOfEnum.GetEnumValues(); var enumNames = typeOfEnum.GetEnumNames(); var count = enumNames.Length; var enumDescriptions = new string[count]; int i = 0; foreach (var item in enumValues) { var name = enumNames[i].Trim(); var fieldInfo = item.GetType().GetField(name); var attributes = (DescriptionAttribute[])fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false); enumDescriptions[i] = (attributes.Length &gt; 0) ? attributes[0].Description : name; i++; } var list = new SelectListItem[count]; for (int index = 0; index &lt; list.Length; index++) { list[index] = new SelectListItem { Value = enumNames[index], Text = enumDescriptions[index], Selected = (index == (selectedItem - 1)) }; } return list; } </code></pre> <p>The end result is a nicely presented DDL.</p> <p>Hope this helps. Any comments about better ways to do this will be greatly appreciated.</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