Reputation: 24894
I have this Enum (Notebook.cs):
public enum Notebook : byte
{
[Display(Name = "Notebook HP")]
NotebookHP,
[Display(Name = "Notebook Dell")]
NotebookDell
}
Also this property in my class (TIDepartment.cs):
public Notebook Notebook { get; set; }
It's working perfectly, I just have one "problem":
I created an EnumDDLFor and it's showing the name I setted in DisplayAttribute, with spaces, but the object doesn't receive that name in DisplayAttribute, receives the Enum name (what is correct), so my question is:
Is there a way to receive the name with spaces which one I configured in DisplayAttribute?
Upvotes: 11
Views: 28517
Reputation: 135
Same as @Chizi here, but with nullability and no fluffer: there's no need for info.CustomAttributes.Any()
since info.GetCustomAttribute<DisplayAttribute>
handles it natively, and info's .First()
can be reworded as [0]
(and can't be null
).
using System.Reflection;
using System.ComponentModel.DataAnnotations;
using System.Resources;
public static string? GetDisplayAttribute(this Enum enumValue)
{
string enumValuation = enumValue.ToString();
DisplayAttribute? nameAttr = enumValue.GetType().GetMember(enumValuation)[0].GetCustomAttribute<DisplayAttribute>();
if (nameAttr?.Name == null) return enumValuation;
// Check for localization
if (nameAttr.ResourceType != null)
// I recommend not newing this up every time for performance
// but rather use a global instance or pass one in
try { return new ResourceManager(nameAttr.ResourceType).GetString(nameAttr.Name); }
// Raised when ResourceType is not part of the assembly's known resources
catch (MissingManifestResourceException) { return nameAttr.Name; }
else
return nameAttr.Name;
}
I use the ResourceType
so I wrapped it in a try..catch
block as it can fail, but you can just drop the "Check for localization" part and keep only the else return nameAttr.Name;
part.
Usage:
public enum Notebook : byte
{
[Display(Name = "Notebook HP", ResourceType = typeof(MyResourceFile))]
NotebookHP,
[Display(Name = "Notebook Dell")]
NotebookDell
}
Console.WriteLine(Notebook.NotebookHP.GetDisplayAttribute());
//Notebook HP
Upvotes: 0
Reputation: 149
While looking at akousmata, I liked everything except the parameters he's passing in. So unless your trying to use this extension for all Enums, I took his code and changed it a bit. While using:
public enum Notebook
{
[Display(Name = "Notebook HP")]
NotebookHP,
[Display(Name = "Notebook Dell")]
NotebookDell
}
Setup the extension:
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;
}
}
To call it:
var n = Notebook.NotebookHP;
Console.WriteLine(n.Display());
Response:
Notebook HP
Upvotes: 0
Reputation: 1043
MVC doesn't make use of the Display attribute on enums (or any framework I'm aware of). You need to create a custom Enum extension class:
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;
}
}
Then you can use it like this:
Notebook n = Notebook.NotebookHP;
String displayName = n.GetDisplayAttributeFrom(typeof(Notebook));
EDIT: Support for localization
This may not be the most efficient way, but SHOULD work.
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;
}
}
On the enum, the key and resource type must be specified:
[Display(Name = "MyResourceKey", ResourceType = typeof(MyResourceFile)]
Upvotes: 11
Reputation: 4293
Here's a simplified (and working) version of akousmata's localized enum extension:
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();
}
Note: I move enumType
from a parameter to a local variable.
Example usage:
public enum IndexGroupBy
{
[Display(Name = "By Alpha")]
ByAlpha,
[Display(Name = "By Type")]
ByType
}
And
@IndexGroupBy.ByAlpha.DisplayName()
Here is a editor template that can be used with the extension method above:
@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;
}
And here is an example usage:
@Html.EditorFor(m => m.YourEnumMember, "Enum_RadioButtonList")
Upvotes: 8
Reputation: 4572
Since you are worrying about visuals I would use a configurable approach:
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.");
}
}
}
Upvotes: 1