2015-04-13 7 views
7

में विवरण एट्रिब्यूट के साथ enum का उपयोग कैसे करें मैं एएसपीनेट एमवीसी के लिए नया हूं। मैं अपने दृश्य पृष्ठ पर ड्रॉपडाउन नियंत्रण का उपयोग करने की कोशिश कर रहा हूं, जो enum से populates। मैं ड्रॉपडाउन मानों में कस्टम विवरण भी जोड़ना चाहता हूं। मैंने इतने सारे उदाहरण खोजे, लेकिन कोई भी पोस्ट पेज पर विवरण को पॉप्युलेट करने के लिए पोस्ट नहीं किया गया।एएसपीनेट एमवीसी

ViewModel:

public enum SearchBy 
    { 
     [Description("SID/PID")] 
     SID = 1, 
     [Description("Name")] 
     Name, 
     [Description("Birth Date")] 
     DOB, 
     [Description("Cause#")] 
     Cause 
    } 

Index.cshtml

<div class="form-horizontal"> 
    @Html.ValidationSummary(true, "", new { @class = "text-danger" }) 
    <div class="form-group form-inline"> 
     @Html.LabelFor(model => model.searchBy, htmlAttributes: new { @class = "control-label col-md-2" }) 
     <div class="col-md-10"> 
      @Html.EnumDropDownListFor(model => model.searchBy, "Search By", htmlAttributes: new { @class = "form-control" }) 
      @Html.TextBox("searchByVal", null, htmlAttributes: new { @placeholder = "SID/PID ", @class = "form-control" }) 
     </div> 
    </div> 

    <div class="form-group"> 
     @Html.LabelFor(model => model.FirstName, htmlAttributes: new { @class = "control-label col-md-2" }) 
     <div class="col-md-10"> 
      @Html.EditorFor(model => model.FirstName, new { htmlAttributes = new { @placeholder = "First Name", @class = "form-control" } }) 
     </div> 
    </div> 

    <div class="form-group"> 
     @Html.LabelFor(model => model.LastName, htmlAttributes: new { @class = "control-label col-md-2" }) 
     <div class="col-md-10"> 
      @Html.EditorFor(model => model.LastName, new { htmlAttributes = new { @placeholder = "Last Name", @class = "form-control" } }) 
     </div> 
    </div> 

    <div class="form-group"> 
     @Html.LabelFor(model => model.DOB, htmlAttributes: new { @class = "control-label col-md-2" }) 
     <div class="col-md-10"> 
      @Html.EditorFor(model => model.DOB, new { htmlAttributes = new { @placeholder = "Birth Date", @class = "form-control" } }) 
     </div> 
    </div> 

    <div class="form-group"> 
     @Html.LabelFor(model => model.CauseNumber, htmlAttributes: new { @class = "control-label col-md-2" }) 
     <div class="col-md-10"> 
      @Html.EditorFor(model => model.CauseNumber, new { htmlAttributes = new { @placeholder = "Cause#", @class = "form-control" } }) 
     </div> 
    </div> 

    <div class="form-group"> 
     <div class="col-md-offset-2 col-md-10"> 
      <input type="submit" value="Search" class="btn btn-block btn-primary" /> 
     </div> 
    </div> 
</div> 

यह मेरी SearchBy enum में उल्लिखित विवरण क्षेत्रों के साथ पॉप्युलेट नहीं है यहाँ मेरी कोड है। यहां छवि देखें। http://postimg.org/image/phdxgocj7/ कृपया मेरी मदद करें, जहां मैं गलती कर रहा हूं। धन्यवाद

अद्यतन: मुझे निको से इसका समाधान मिला। और मैंने इस पर थोड़ा सा शोध किया। मैं इस पोस्ट को समाधान के साथ अद्यतन कर रहा हूं क्योंकि यह दूसरों के लिए उपयोगी हो सकता है, जो एमवीसी http://weblogs.asp.net/jongalloway//looking-at-asp-net-mvc-5-1-and-web-api-2-1-part-1-overview-and-enums

सभी के लिए उपयोगी हो सकता है। कोडिंग का आनंद लें ..

+0

कोई 'EnumDropDownListFor()' MVC में विधि नहीं है -4। क्या आपका मतलब एमवीसी -5 है या यह एक कस्टम सहायक है? –

+0

क्षमा करें, मैंने टैग अपडेट किया है। हां, मैं एमवीसी -5 –

उत्तर

16

एचटीएमएल सहायक EnumDropDownListFor या EnumDropDownList विचार Description विशेषता enum सदस्यों पर सजावट पर ध्यान नहीं देता। हालांकि स्रोत कोड की समीक्षा द्वारा:

Enum ड्रापडाउन सूची हेल्पर: https://aspnetwebstack.codeplex.com/SourceControl/latest#src/System.Web.Mvc/Html/SelectExtensions.cs

Enum हेल्पर क्लास: https://aspnetwebstack.codeplex.com/SourceControl/latest#src/System.Web.Mvc/Html/EnumHelper.cs

enum सहायक ऊपर कक्षाएं एक List<SelectListItem> के लिए एक Enum कन्वर्ट करने के लिए उपयोग किया जाता है। नीचे दिए गए कोड से:

// Return non-empty name specified in a [Display] attribute for the given field, if any; field's name otherwise 
private static string GetDisplayName(FieldInfo field) 
{ 
    DisplayAttribute display = field.GetCustomAttribute<DisplayAttribute>(inherit: false); 
    if (display != null) 
    { 
     string name = display.GetName(); 
     if (!String.IsNullOrEmpty(name)) 
     { 
      return name; 
     } 
    } 

    return field.Name; 
} 

आप विधि में देख सकते हैं कि GetDisplayName यह enum सदस्य पर DisplayAttribute के अस्तित्व के लिए जाँच करता है। यदि प्रदर्शन विशेषता मौजूद है तो नाम DisplayAttribute.GetName() विधि के परिणाम पर सेट है।

इस एक साथ हम कर सकते हैं लाना enum संशोधित DescriptionAttribute के बजाय DisplayAttribute उपयोग करने के लिए और मूल्य आप दिखाए जाने वाले Name संपत्ति की स्थापना।

public enum SearchBy 
{ 
    [Display(Name = "SID/PID")] 
    SID = 1, 
    [Display(Name = "Name")] 
    Name, 
    [Display(Name = "Birth Date")] 
    DOB, 
    [Display(Name = "Cause#")] 
    Cause 
} 

यह आपको इच्छित परिणाम देता है।

enter image description here

आशा इस मदद करता है।

+0

का उपयोग कर रहा हूं धन्यवाद निको। यह मदद करता है, लेकिन दृश्य पृष्ठ में, मुझे EnumDropDownListFor के साथ क्या बदलना चाहिए? –

+0

थोड़ा सा शोध करने के बाद, मैं समझता हूं कि, मुझे ड्रॉप डाउन के रूप में देखने के लिए एनम को सही ढंग से प्रस्तुत करने के लिए प्रदर्शन टेम्पलेट लिखना है, सही? अगर मैं ग़लत हूं तो मेरी गलती सुझाएं। मुझे यहां इस पर एक लेख मिला: http://www.codeproject.com/Articles/776908/Dealing-with-Enum-in-MVC –

1

को देखते हुए:

public enum MyEnum 
{ 
    [Description("This is the description if my member A")] 
    A, 
    [Description("This is the description if my member B")] 
    B 
} 

मैं व्यक्तिगत रूप से मेरी enum से एक विवरण प्राप्त करने के लिए इस विस्तार विधि का उपयोग करें:

public static string GetDescription(this Enum value) 
     { 
      FieldInfo fi = value.GetType().GetField(value.ToString()); 

      DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false); 

      if (attributes != null && attributes.Length > 0) 
      { 
       return attributes[0].Description; 
      } 
      else 
      { 
       return value.ToString(); 
      } 
     } 

इसका इस्तेमाल करने के लिए:

MyEnum.A.GetDescription(); 

आशा इस मदद करते हैं।

+0

धन्यवाद एंड्रयू। निको ने मुझे इस मुद्दे को हल करने में मदद की। –

0

मैंने एक सहायक वर्ग बनाया जो विभिन्न प्रकार के विशेषताओं की कोशिश करता है। क्योंकि मैं https://github.com/civicsource/enums और https://silviomoreto.github.io/bootstrap-select/

public static class EnumHelper<T> 
    { 
     static EnumHelper() 
     { 
      var enumType = typeof(T); 
      if (!enumType.IsEnum) { throw new ArgumentException("Type '" + enumType.Name + "' is not an enum"); } 
     } 

     public static string GetEnumDescription(T value) 
     { 
      var fi = typeof(T).GetField(value.ToString()); 
      var attributes = (DescriptionAttribute[]) fi.GetCustomAttributes(typeof(DescriptionAttribute), false); 
      return attributes.Length > 0 ? attributes[0].Description : value.ToString(); 
     } 

     public static IEnumerable<SelectListItem> GetSelectList() 
     { 
      var groupDictionary = new Dictionary<string, SelectListGroup>(); 

      var enumType = typeof(T); 
      var fields = from field in enumType.GetFields() 
         where field.IsLiteral 
         select field; 

      foreach (var field in fields) 
      { 
       var display = field.GetCustomAttribute<DisplayAttribute>(false); 
       var description = field.GetCustomAttribute<DescriptionAttribute>(false); 
       var group = field.GetCustomAttribute<CategoryAttribute>(false); 

       var text = display?.GetName() ?? display?.GetShortName() ?? display?.GetDescription() ?? display?.GetPrompt() ?? description?.Description ?? field.Name; 
       var value = field.Name; 
       var groupName = display?.GetGroupName() ?? group?.Category ?? string.Empty; 
       if (!groupDictionary.ContainsKey(groupName)) { groupDictionary.Add(groupName, new SelectListGroup { Name = groupName }); } 

       yield return new SelectListItem 
       { 
        Text = text, 
        Value = value, 
        Group = groupDictionary[groupName], 
       }; 
      } 
     } 
    } 

साथ बूटस्ट्रैप उपयोग कर रहा था मैं यह आवश्यक और आप इसे कॉल की तरह:

<div class="form-group"> 
    @Html.LabelFor(model => model.Address.State, htmlAttributes: new { @class = "control-label col-md-2" }) 
    <div class="col-sm-4"> 
     @Html.DropDownListFor(model => model.Address.State, EnumHelper<StateProvince>.GetSelectList(), new { @class = "selectpicker show-menu-arrow", data_live_search = "true" }) 
     @Html.ValidationMessageFor(model => model.Address.State, "", new { @class = "text-danger" }) 
    </div> 
</div> 

Result