我有这个枚举(Notebook.cs):
public enum Notebook : byte
{
[Display(Name = "Notebook HP")]
NotebookHP,
[Display(Name = "Notebook Dell")]
NotebookDell
}
我的班级中还有这个属性(TIDepartment.cs):
public Notebook Notebook { get; set; }
它工作得很好,我只有一个“问题”:
我创建了一个 EnumDDLFor ,它显示了我在 DisplayAttribute 中设置的名称,带有空格,但该对象没有在 DisplayAttribute 中接收该名称,而是接收 Enum 名称(正确的),所以我的问题是:
有没有一种方法可以接收我在 DisplayAttribute 中配置的带有空格的名称?
MVC 不使用枚举(或我知道的任何框架)上的 Display 属性。您需要创建一个自定义 Enum 扩展类:
public static class EnumExtensions
{
public static string GetDisplayAttributeFrom(this Enum enumValue, Type enumType)
{
string displayName = "";
MemberInfo info = enumType.GetMember(enumValue.ToString()).First();
if (info != null && info.CustomAttributes.Any())
{
DisplayAttribute nameAttr = info.GetCustomAttribute<DisplayAttribute>();
displayName = nameAttr != null ? nameAttr.Name : enumValue.ToString();
}
else
{
displayName = enumValue.ToString();
}
return displayName;
}
}
然后你可以像这样使用它:
Notebook n = Notebook.NotebookHP;
String displayName = n.GetDisplayAttributeFrom(typeof(Notebook));
编辑:支持本地化
这可能不是最有效的方法,但应该有效。
public static class EnumExtensions
{
public static string GetDisplayAttributeFrom(this Enum enumValue, Type enumType)
{
string displayName = "";
MemberInfo info = enumType.GetMember(enumValue.ToString()).First();
if (info != null && info.CustomAttributes.Any())
{
DisplayAttribute nameAttr = info.GetCustomAttribute<DisplayAttribute>();
if(nameAttr != null)
{
// Check for localization
if(nameAttr.ResourceType != null && nameAttr.Name != null)
{
// I recommend not newing this up every time for performance
// but rather use a global instance or pass one in
var manager = new ResourceManager(nameAttr.ResourceType);
displayName = manager.GetString(nameAttr.Name)
}
else if (nameAttr.Name != null)
{
displayName = nameAttr != null ? nameAttr.Name : enumValue.ToString();
}
}
}
else
{
displayName = enumValue.ToString();
}
return displayName;
}
}
在枚举上,必须指定键和资源类型:
[Display(Name = "MyResourceKey", ResourceType = typeof(MyResourceFile)]
这是 akousmata 本地化枚举扩展的简化(且有效)版本:
public static string DisplayName(this Enum enumValue)
{
var enumType = enumValue.GetType();
var memberInfo = enumType.GetMember(enumValue.ToString()).First();
if (memberInfo == null || !memberInfo.CustomAttributes.Any()) return enumValue.ToString();
var displayAttribute = memberInfo.GetCustomAttribute<DisplayAttribute>();
if (displayAttribute == null) return enumValue.ToString();
if (displayAttribute.ResourceType != null && displayAttribute.Name != null)
{
var manager = new ResourceManager(displayAttribute.ResourceType);
return manager.GetString(displayAttribute.Name);
}
return displayAttribute.Name ?? enumValue.ToString();
}
注意:我将
enumType
从参数移至局部变量。
使用示例:
public enum IndexGroupBy
{
[Display(Name = "By Alpha")]
ByAlpha,
[Display(Name = "By Type")]
ByType
}
还有
@IndexGroupBy.ByAlpha.DisplayName()
这里是一个编辑器模板,可以与上面的扩展方法一起使用:
@model Enum
@{
var listItems = Enum.GetValues(Model.GetType()).OfType<Enum>().Select(e =>
new SelectListItem
{
Text = e.DisplayName(),
Value = e.ToString(),
Selected = e.Equals(Model)
});
var prefix = ViewData.TemplateInfo.HtmlFieldPrefix;
var index = 0;
ViewData.TemplateInfo.HtmlFieldPrefix = string.Empty;
foreach (var li in listItems)
{
var fieldName = string.Format(CultureInfo.InvariantCulture, "{0}_{1}", prefix, index++);
<div class="editor-radio">
@Html.RadioButton(prefix, li.Value, li.Selected, new {@id = fieldName})
@Html.Label(fieldName, li.Text)
</div>
}
ViewData.TemplateInfo.HtmlFieldPrefix = prefix;
}
这是一个示例用法:
@Html.EditorFor(m => m.YourEnumMember, "Enum_RadioButtonList")
既然您担心视觉效果,我会使用可配置的方法:
public NotebookTypes NotebookType;
public enum NotebookTypes{
NotebookHP,
NotebookDell
}
public string NotebookTypeName{
get{
switch(NotebookType){
case NotebookTypes.NotebookHP:
return "Notebook HP"; //You may read the language dependent value from xml...
case NotebookTypes.NotebookDell:
return "Notebook Dell"; //You may read the language dependent value from xml...
default:
throw new NotImplementedException("'" + typeof(NotebookTypes).Name + "." + NotebookType.ToString() + "' is not implemented correctly.");
}
}
}
在查看 akousmata 时,我喜欢除了他传入的参数之外的所有内容。因此,除非您尝试对所有枚举使用此扩展,否则我会采用他的代码并对其进行一些更改。 使用时:
public enum Notebook
{
[Display(Name = "Notebook HP")]
NotebookHP,
[Display(Name = "Notebook Dell")]
NotebookDell
}
设置扩展:
public static class EnumExtensions
{
public static string Display(this Notebook enumValue)
{
string displayName;
var info = enumValue.GetType().GetMember(enumValue.ToString()).First();
if (info != null && info.CustomAttributes.Any())
{
var nameAttr = info.GetCustomAttribute<DisplayAttribute>();
displayName = nameAttr != null ? nameAttr.Name : enumValue.ToString();
}
else
{
displayName = enumValue.ToString();
}
return displayName;
}
}
称呼它:
var n = Notebook.NotebookHP;
Console.WriteLine(n.Display());
回应:
Notebook HP