并将其放在Enum.ascx名称下的EditorTemplates文件夹中? 这是我尝试解决的问题的一种解决方法,但这不是-6ren">
gpt4 book ai didi

asp.net - 如何为枚举创建默认的编辑器模板?

转载 作者:行者123 更新时间:2023-12-03 12:08:02 25 4
gpt4 key购买 nike

如何为枚举创建默认的编辑器模板?我的意思是:我可以做这样的事情:

<%@ 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文件夹中。这是模板:
<%@ 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模板,而不是每次都指定一个UIHint,并且可以通过创建一个自定义模型元数据提供程序来实现这一点,如下所示:

using System;
using System.Collections.Generic;
using System.Web.Mvc;

public class CustomMetadataProvider : DataAnnotationsModelMetadataProvider
{
protected override ModelMetadata CreateMetadata(IEnumerable<Attribute> attributes, Type containerType, Func<object> modelAccessor, Type modelType, string propertyName) {
var mm = base.CreateMetadata(attributes, containerType, modelAccessor, modelType, propertyName);
if (modelType.IsEnum && mm.TemplateHint == null) {
mm.TemplateHint = "Enum";
}
return mm;
}
}

然后只需在Global.asax.cs的Application_Start方法中注册它即可:
ModelMetadataProviders.Current = new CustomMetadataProvider();

现在,默认情况下,所有枚举属性都将使用您的Enum模板。

关于asp.net - 如何为枚举创建默认的编辑器模板?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3431515/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com