मैं enums के लिए एक डिफ़ॉल्ट संपादक टेम्पलेट कैसे बना सकता हूं? जो से मेरा मतलब है: मैं कुछ इस तरह कर सकते हैं:enums के लिए एक डिफ़ॉल्ट संपादक टेम्पलेट कैसे बनाएँ?
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Enum>" %>
<% -- any code to read the enum and write a dropdown -->
और नाम Enum.ascx
तहत EditorTemplates फ़ोल्डर में डाल?
मेरी समस्या के लिए यहां एक कामकाज है जिसकी मैंने कोशिश की, लेकिन मुझे इसकी आवश्यकता नहीं है।
public enum GenderEnum
{
/// <summary>
/// Male
/// </summary>
[Description("Male Person")]
Male,
/// <summary>
/// Female
/// </summary>
[Description("Female Person")]
Female
}
मैं GenderEnum.acsx
नामक एक टेम्पलेट बना दिया है और Shared/EditorTemplates
फ़ोल्डर में रख:
यहाँ मेरी Enum है।
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<AlefTech.HumanResource.Core.GenderEnum>" %>
<%@ Import Namespace="AlefTech.HumanResource.WebModule.Classes" %>
<%=Html.DropDownListFor(m => m.GetType().Name, Model.GetType()) %>
बेशक विधि अपने ही है:
public static class HtmlHelperExtension
{
public static MvcHtmlString DropDownListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, Type enumType)
{
List<SelectListItem> list = new List<SelectListItem>();
Dictionary<string, string> enumItems = enumType.GetDescription();
foreach (KeyValuePair<string, string> pair in enumItems)
list.Add(new SelectListItem() { Value = pair.Key, Text = pair.Value });
return htmlHelper.DropDownListFor(expression, list);
}
/// <summary>
/// return the items of enum paired with its descrtioption.
/// </summary>
/// <param name="enumeration">enumeration type to be processed.</param>
/// <returns></returns>
public static Dictionary<string, string> GetDescription(this Type enumeration)
{
if (!enumeration.IsEnum)
{
throw new ArgumentException("passed type must be of Enum type", "enumerationValue");
}
Dictionary<string, string> descriptions = new Dictionary<string, string>();
var members = enumeration.GetMembers().Where(m => 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;
}
}
हालांकि भले ही यह मेरे लिए काम किया, है, यह मैं क्या पूछ रहा हूँ नहीं है यहाँ खाका है।
कोड Shared\EditorTemplates\Enum.acsx
के लिए: इसके बजाय, मैं काम करने के लिए निम्नलिखित की जरूरत है
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Enum>" %>
<%@ Import Namespace="System.Web.Mvc.Html" %>
<%@ Import Namespace="WhereMyExtentionMethod" %>
<%=Html.DropDownListFor(m => m.GetType().Name, Model.GetType()) %>
इस के साथ मैं हर किसी भी अधिक enum के लिए खाका बनाने के लिए नहीं होगा।
क्या आपको अभी भी यह काम करने के लिए नहीं मिला? क्या आप यहां इस्तेमाल किए गए सहायक के लिए कोड पोस्ट करना चाहते हैं: htmlHelper.DropDownListFor (अभिव्यक्ति, सूची) ;? –
वैसे, यदि आप अपने मॉडल में अपने enum-field पर [UIHint ("Enum") डालते हैं, तो आपके कोड को काम करना चाहिए, सिस्टम.Web.Mvc.ViewUserControl को डायनामिक में बदलें, और उन्हें सही प्रकार के रूप में डालें सहायक कॉल में :) –