How to get display value from enum in C#? [duplicate] - c#
I've got a property in my model called Promotion that its type is a flag enum called UserPromotion. Members of my enum have display attributes set as follows:
[Flags]
public enum UserPromotion
{
None = 0x0,
[Display(Name = "Send Job Offers By Mail")]
SendJobOffersByMail = 0x1,
[Display(Name = "Send Job Offers By Sms")]
SendJobOffersBySms = 0x2,
[Display(Name = "Send Other Stuff By Sms")]
SendPromotionalBySms = 0x4,
[Display(Name = "Send Other Stuff By Mail")]
SendPromotionalByMail = 0x8
}
Now I want to be able to create say a ul in my view to show the selected values of my Promotion property. This is what I have done so far but the problem is that how can I get the display names here?
<ul>
#foreach (int aPromotion in #Enum.GetValues(typeof(UserPromotion)))
{
var currentPromotion = (int)Model.JobSeeker.Promotion;
if ((currentPromotion & aPromotion) == aPromotion)
{
<li>Here I don't know how to get the display attribute of "currentPromotion".</li>
}
}
</ul>
One liner - Fluent syntax
public static class Extensions
{
/// <summary>
/// A generic extension method that aids in reflecting
/// and retrieving any attribute that is applied to an `Enum`.
/// </summary>
public static TAttribute GetAttribute<TAttribute>(this Enum enumValue)
where TAttribute : Attribute
{
return enumValue.GetType()
.GetMember(enumValue.ToString())
.First()
.GetCustomAttribute<TAttribute>();
}
}
Example
public enum Season
{
[Display(Name = "It's autumn")]
Autumn,
[Display(Name = "It's winter")]
Winter,
[Display(Name = "It's spring")]
Spring,
[Display(Name = "It's summer")]
Summer
}
public class Foo
{
public Season Season = Season.Summer;
public void DisplayName()
{
var seasonDisplayName = Season.GetAttribute<DisplayAttribute>();
Console.WriteLine("Which season is it?");
Console.WriteLine (seasonDisplayName.Name);
}
}
Output
Which season is it?
It's summer
UPDATE
First solution was focused on getting display names from enum. Code below should be exact solution for your problem.
You can use this helper class for enums:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Reflection;
public static class EnumHelper<T>
where T : struct, Enum // This constraint requires C# 7.3 or later.
{
public static IList<T> GetValues(Enum value)
{
var enumValues = new List<T>();
foreach (FieldInfo fi in value.GetType().GetFields(BindingFlags.Static | BindingFlags.Public))
{
enumValues.Add((T)Enum.Parse(value.GetType(), fi.Name, false));
}
return enumValues;
}
public static T Parse(string value)
{
return (T)Enum.Parse(typeof(T), value, true);
}
public static IList<string> GetNames(Enum value)
{
return value.GetType().GetFields(BindingFlags.Static | BindingFlags.Public).Select(fi => fi.Name).ToList();
}
public static IList<string> GetDisplayValues(Enum value)
{
return GetNames(value).Select(obj => GetDisplayValue(Parse(obj))).ToList();
}
private static string lookupResource(Type resourceManagerProvider, string resourceKey)
{
var resourceKeyProperty = resourceManagerProvider.GetProperty(resourceKey,
BindingFlags.Static | BindingFlags.Public, null, typeof(string),
new Type[0], null);
if (resourceKeyProperty != null)
{
return (string)resourceKeyProperty.GetMethod.Invoke(null, null);
}
return resourceKey; // Fallback with the key name
}
public static string GetDisplayValue(T value)
{
var fieldInfo = value.GetType().GetField(value.ToString());
var descriptionAttributes = fieldInfo.GetCustomAttributes(
typeof(DisplayAttribute), false) as DisplayAttribute[];
if (descriptionAttributes[0].ResourceType != null)
return lookupResource(descriptionAttributes[0].ResourceType, descriptionAttributes[0].Name);
if (descriptionAttributes == null) return string.Empty;
return (descriptionAttributes.Length > 0) ? descriptionAttributes[0].Name : value.ToString();
}
}
And then you can use it in your view as following:
<ul>
#foreach (var value in #EnumHelper<UserPromotion>.GetValues(UserPromotion.None))
{
if (value == Model.JobSeeker.Promotion)
{
var description = EnumHelper<UserPromotion>.GetDisplayValue(value);
<li>#Html.DisplayFor(e => description )</li>
}
}
</ul>
Building on Aydin's great answer, here's an extension method that doesn't require any type parameters.
using System;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Reflection;
public static class EnumExtensions
{
public static string GetDisplayName(this Enum enumValue)
{
return enumValue.GetType()
.GetMember(enumValue.ToString())
.First()
.GetCustomAttribute<DisplayAttribute>()
.GetName();
}
}
NOTE: GetName() should be used instead of the Name property. This ensures that the localized string will be returned if using the ResourceType attribute property.
Example
To use it, just reference the enum value in your view.
#{
UserPromotion promo = UserPromotion.SendJobOffersByMail;
}
Promotion: #promo.GetDisplayName()
Output
Promotion: Send Job Offers By Mail
Based on Aydin's answer I would suggest a less "duplicatious" implementation (because we could easily get the Type from the Enum value itself, instead of providing it as a parameter 馃槈:
using System;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Reflection;
public static string GetDisplayName(this Enum enumValue)
{
return enumValue.GetType().GetMember(enumValue.ToString())
.First()
.GetCustomAttribute<DisplayAttribute>()
.Name;
}
EDIT (based upon #Vahagn Nahapetyan's comment)
public static string GetDisplayName(this Enum enumValue)
{
return enumValue.GetType()?
.GetMember(enumValue.ToString())?
.First()?
.GetCustomAttribute<DisplayAttribute>()?
.Name;
}
Now we can use it very clean in this way:
public enum Season
{
[Display(Name = "The Autumn")]
Autumn,
[Display(Name = "The Weather")]
Winter,
[Display(Name = "The Tease")]
Spring,
[Display(Name = "The Dream")]
Summer
}
Season.Summer.GetDisplayName();
Which results in
"The Dream"
If you are using MVC 5.1 or upper there is simplier and clearer way: just use data annotation (from System.ComponentModel.DataAnnotations namespace) like below:
public enum Color
{
[Display(Name = "Dark red")]
DarkRed,
[Display(Name = "Very dark red")]
VeryDarkRed,
[Display(Name = "Red or just black?")]
ReallyDarkRed
}
And in view, just put it into proper html helper:
#Html.EnumDropDownListFor(model => model.Color)
Building on Todd's great answer which built on Aydin's great answer, here's a generic extension method which doesn't require any type parameters.
/// <summary>
/// Gets human-readable version of enum.
/// </summary>
/// <returns>effective DisplayAttribute.Name of given enum.</returns>
public static string GetDisplayName<T>(this T enumValue) where T : IComparable, IFormattable, IConvertible // C# 7.3+: where T : struct, Enum
{
if (!typeof(T).IsEnum) // Not needed in C# 7.3+ with above updated constraint
throw new ArgumentException("Argument must be of type Enum");
DisplayAttribute displayAttribute = enumValue.GetType()
.GetMember(enumValue.ToString())
.First()
.GetCustomAttribute<DisplayAttribute>();
string displayName = displayAttribute?.GetName();
return displayName ?? enumValue.ToString();
}
I needed this for my project because something like the below code, where not every member of the enum has a DisplayAttribute, throws an exception with Todd's solution:
public class MyClass
{
public enum MyEnum
{
[Display(Name="ONE")]
One,
// No DisplayAttribute
Two
}
public void UseMyEnum()
{
MyEnum foo = MyEnum.One;
MyEnum bar = MyEnum.Two;
Console.WriteLine(foo.GetDisplayName());
Console.WriteLine(bar.GetDisplayName());
}
}
// Output:
//
// ONE
// Two
If this is a complicated solution to a simple problem, please let me know, but this was the fix I used.
You could use Type.GetMember Method, then get the attribute info using reflection:
// display attribute of "currentPromotion"
var type = typeof(UserPromotion);
var memberInfo = type.GetMember(currentPromotion.ToString());
var attributes = memberInfo[0].GetCustomAttributes(typeof(DisplayAttribute), false);
var description = ((DisplayAttribute)attributes[0]).Name;
There were a few similar posts here:
Getting attributes of Enum's value
How to make MVC3 DisplayFor show the value of an Enum's Display-Attribute?
In .NET5, I used DisplayTextFor without needing helper or extension methods:
#Html.DisplayTextFor(m => m.SomeEnumProperty)
Where SomeEnumProperty has a value of:
public enum MyEnum
{
[Display(Name = "Not started")]
NotStarted = 0,
[Display(Name = "Weird display name instead of just 'Started'")]
Started = 1,
}
For ASP.Net Core 3.0, this worked for me (credit to previous answerers).
My Enum class:
using System;
using System.Linq;
using System.ComponentModel.DataAnnotations;
using System.Reflection;
public class Enums
{
public enum Duration
{
[Display(Name = "1 Hour")]
OneHour,
[Display(Name = "1 Day")]
OneDay
}
// Helper method to display the name of the enum values.
public static string GetDisplayName(Enum value)
{
return value.GetType()?
.GetMember(value.ToString())?.First()?
.GetCustomAttribute<DisplayAttribute>()?
.Name;
}
}
My View Model Class:
public class MyViewModel
{
public Duration Duration { get; set; }
}
An example of a razor view displaying a label and a drop-down list. Notice the drop-down list does not require a helper method:
#model IEnumerable<MyViewModel>
#foreach (var item in Model)
{
<label asp-for="#item.Duration">#Enums.GetDisplayName(item.Duration)</label>
<div class="form-group">
<label asp-for="#item.Duration" class="control-label">Select Duration</label>
<select asp-for="#item.Duration" class="form-control"
asp-items="Html.GetEnumSelectList<Enums.Duration>()">
</select>
</div>
}
With Core 2.1,
public static string GetDisplayName(Enum enumValue)
{
return enumValue.GetType()?
.GetMember(enumValue.ToString())?[0]?
.GetCustomAttribute<DisplayAttribute>()?
.Name;
}
<ul>
#foreach (int aPromotion in #Enum.GetValues(typeof(UserPromotion)))
{
var currentPromotion = (int)Model.JobSeeker.Promotion;
if ((currentPromotion & aPromotion) == aPromotion)
{
<li>#Html.DisplayFor(e => currentPromotion)</li>
}
}
</ul>
combining all edge-cases together from above:
enum members with base object members' names (Equals, ToString)
optional Display attribute
here is my code:
public enum Enum
{
[Display(Name = "What a weird name!")]
ToString,
Equals
}
public static class EnumHelpers
{
public static string GetDisplayName(this Enum enumValue)
{
var enumType = enumValue.GetType();
return enumType
.GetMember(enumValue.ToString())
.Where(x => x.MemberType == MemberTypes.Field && ((FieldInfo)x).FieldType == enumType)
.First()
.GetCustomAttribute<DisplayAttribute>()?.Name ?? enumValue.ToString();
}
}
void Main()
{
Assert.Equals("What a weird name!", Enum.ToString.GetDisplayName());
Assert.Equals("Equals", Enum.Equals.GetDisplayName());
}
You need to use a bit of reflection in order to access that attribute:
var type = typeof(UserPromotion);
var member = type.GetMember(Model.JobSeeker.Promotion.ToString());
var attributes = member[0].GetCustomAttributes(typeof(DisplayAttribute), false);
var name = ((DisplayAttribute)attributes[0]).Name;
I recommend wrapping this method in a extension method or perform this in a view model.
I'm sorry to do this, but I couldn't use any of the other answers as-is and haven't time to duke it out in the comments.
Uses C# 6 syntax.
static class EnumExtensions
{
/// returns the localized Name, if a [Display(Name="Localised Name")] attribute is applied to the enum member
/// returns null if there isnt an attribute
public static string DisplayNameOrEnumName(this Enum value)
// => value.DisplayNameOrDefault() ?? value.ToString()
{
// More efficient form of ^ based on http://stackoverflow.com/a/17034624/11635
var enumType = value.GetType();
var enumMemberName = Enum.GetName(enumType, value);
return enumType
.GetEnumMemberAttribute<DisplayAttribute>(enumMemberName)
?.GetName() // Potentially localized
?? enumMemberName; // Or fall back to the enum name
}
/// returns the localized Name, if a [Display] attribute is applied to the enum member
/// returns null if there is no attribute
public static string DisplayNameOrDefault(this Enum value) =>
value.GetEnumMemberAttribute<DisplayAttribute>()?.GetName();
static TAttribute GetEnumMemberAttribute<TAttribute>(this Enum value) where TAttribute : Attribute =>
value.GetType().GetEnumMemberAttribute<TAttribute>(value.ToString());
static TAttribute GetEnumMemberAttribute<TAttribute>(this Type enumType, string enumMemberName) where TAttribute : Attribute =>
enumType.GetMember(enumMemberName).Single().GetCustomAttribute<TAttribute>();
}
Building further on Aydin's and Todd's answers, here is an extension method that also lets you get the name from a resource file
using AppResources;
using System;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Reflection;
using System.Resources;
public static class EnumExtensions
{
public static string GetDisplayName(this Enum enumValue)
{
var enumMember= enumValue.GetType()
.GetMember(enumValue.ToString());
DisplayAttribute displayAttrib = null;
if (enumMember.Any()) {
displayAttrib = enumMember
.First()
.GetCustomAttribute<DisplayAttribute>();
}
string name = null;
Type resource = null;
if (displayAttrib != null)
{
name = displayAttrib.Name;
resource = displayAttrib.ResourceType;
}
return String.IsNullOrEmpty(name) ? enumValue.ToString()
: resource == null ? name
: new ResourceManager(resource).GetString(name);
}
}
and use it like
public enum Season
{
[Display(ResourceType = typeof(Resource), Name = Season_Summer")]
Summer
}
For just displaying enum's display name attribute just use
Microsoft.AspNetCore.Mvc.Rendering's
#Html.DisplayFor(x => EnumType.EnumValue)
That's would be enough.
For displaying SelectList write as following:
<select id="someIdForTheEndPoint" asp-items="Html.GetEnumSelectList<EnumType>()">
<option selected="selected" value="">Select value</option>
</select>
I have two solutions for this Question.
The first solution is on getting display names from enum.
public enum CourseLocationTypes
{
[Display(Name = "On Campus")]
OnCampus,
[Display(Name = "Online")]
Online,
[Display(Name = "Both")]
Both
}
public static string DisplayName(this Enum value)
{
Type enumType = value.GetType();
string enumValue = Enum.GetName(enumType, value);
MemberInfo member = enumType.GetMember(enumValue)[0];
object[] attrs = member.GetCustomAttributes(typeof(DisplayAttribute), false);
string outString = ((DisplayAttribute)attrs[0]).Name;
if (((DisplayAttribute)attrs[0]).ResourceType != null)
{
outString = ((DisplayAttribute)attrs[0]).GetName();
}
return outString;
}
<h3 class="product-title white">#Model.CourseLocationType.DisplayName()</h3>
The second Solution is on getting display name from enum name but that will be enum split in developer language it's called patch.
public static string SplitOnCapitals(this string text)
{
var r = new Regex(#"
(?<=[A-Z])(?=[A-Z][a-z]) |
(?<=[^A-Z])(?=[A-Z]) |
(?<=[A-Za-z])(?=[^A-Za-z])", RegexOptions.IgnorePatternWhitespace);
return r.Replace(text, " ");
}
<div class="widget-box pt-0">
#foreach (var item in Enum.GetNames(typeof(CourseLocationType)))
{
<label class="pr-2 pt-1">
#Html.RadioButtonFor(x => x.CourseLocationType, item, new { type = "radio", #class = "iCheckBox control-label" }) #item.SplitOnCapitals()
</label>
}
#Html.ValidationMessageFor(x => x.CourseLocationType)
</div>
2020 Update: An updated version of the function provided by many in this thread but now for C# 7.3 onwards:
Now you can restrict generic methods to enums types so you can write a single method extension to use it with all your enums like this:
The generic extension method:
public static string ATexto<T>(this T enumeraci贸n) where T : struct, Enum {
var tipo = enumeraci贸n.GetType();
return tipo.GetMember(enumeraci贸n.ToString())
.Where(x => x.MemberType == MemberTypes.Field && ((FieldInfo)x).FieldType == tipo).First()
.GetCustomAttribute<DisplayAttribute>()?.Name ?? enumeraci贸n.ToString();
}
The enum:
public enum TipoImpuesto {
IVA, INC, [Display(Name = "IVA e INC")]IVAeINC, [Display(Name = "No aplica")]NoAplica };
How to use it:
var tipoImpuesto = TipoImpuesto.IVAeINC;
var textoTipoImpuesto = tipoImpuesto.ATexto(); // Prints "IVA e INC".
Bonus, Enums with Flags: If you are dealing with normal enums the function above is enough, but if any of your enums can take multiple values with the use of flags then you will need to modify it like this (This code uses C#8 features):
public static string ATexto<T>(this T enumeraci贸n) where T : struct, Enum {
var tipo = enumeraci贸n.GetType();
var textoDirecto = enumeraci贸n.ToString();
string obtenerTexto(string textoDirecto) => tipo.GetMember(textoDirecto)
.Where(x => x.MemberType == MemberTypes.Field && ((FieldInfo)x).FieldType == tipo)
.First().GetCustomAttribute<DisplayAttribute>()?.Name ?? textoDirecto;
if (textoDirecto.Contains(", ")) {
var texto = new StringBuilder();
foreach (var textoDirectoAux in textoDirecto.Split(", ")) {
texto.Append($"{obtenerTexto(textoDirectoAux)}, ");
}
return texto.ToString()[0..^2];
} else {
return obtenerTexto(textoDirecto);
}
}
The enum with flags:
[Flags] public enum TipoContribuyente {
[Display(Name = "Com煤n")] Com煤n = 1,
[Display(Name = "Gran Contribuyente")] GranContribuyente = 2,
Autorretenedor = 4,
[Display(Name = "Retenedor de IVA")] RetenedorIVA = 8,
[Display(Name = "R茅gimen Simple")] R茅gimenSimple = 16 }
How to use it:
var tipoContribuyente = TipoContribuyente.RetenedorIVA | TipoContribuyente.GranContribuyente;
var textoAux = tipoContribuyente.ATexto(); // Prints "Gran Contribuyente, Retenedor de IVA".
I want to contribute with culture-dependent GetDisplayName enum extension. Hope this will be usefull for anyone googling this answer like me previously:
"standart" way as Aydin Adn and Todd mentioned:
public static string GetDisplayName(this Enum enumValue)
{
return enumValue
.GetType()
.GetMember(enumValue.ToString())
.First()
.GetCustomAttribute<DisplayAttribute>()
.GetName();
}
"Culture-dependent" way:
public static string GetDisplayName(this Enum enumValue, CultureInfo ci)
{
var displayAttr = enumValue
.GetType()
.GetMember(enumValue.ToString())
.First()
.GetCustomAttribute<DisplayAttribute>();
var resMan = displayAttr.ResourceType?.GetProperty(#"ResourceManager", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic).GetValue(null, null) as ResourceManager;
return resMan?.GetString(displayAttr.Name, ci) ?? displayAttr.GetName();
}
It is maybe cheating, but it's works:
#foreach (var yourEnum in Html.GetEnumSelectList<YourEnum>())
{
#yourEnum.Text
}
Based on previous answers I've created this comfortable helper to support all DisplayAttribute properties in a readable way:
public static class EnumExtensions
{
public static DisplayAttributeValues GetDisplayAttributeValues(this Enum enumValue)
{
var displayAttribute = enumValue.GetType().GetMember(enumValue.ToString()).First().GetCustomAttribute<DisplayAttribute>();
return new DisplayAttributeValues(enumValue, displayAttribute);
}
public sealed class DisplayAttributeValues
{
private readonly Enum enumValue;
private readonly DisplayAttribute displayAttribute;
public DisplayAttributeValues(Enum enumValue, DisplayAttribute displayAttribute)
{
this.enumValue = enumValue;
this.displayAttribute = displayAttribute;
}
public bool? AutoGenerateField => this.displayAttribute?.GetAutoGenerateField();
public bool? AutoGenerateFilter => this.displayAttribute?.GetAutoGenerateFilter();
public int? Order => this.displayAttribute?.GetOrder();
public string Description => this.displayAttribute != null ? this.displayAttribute.GetDescription() : string.Empty;
public string GroupName => this.displayAttribute != null ? this.displayAttribute.GetGroupName() : string.Empty;
public string Name => this.displayAttribute != null ? this.displayAttribute.GetName() : this.enumValue.ToString();
public string Prompt => this.displayAttribute != null ? this.displayAttribute.GetPrompt() : string.Empty;
public string ShortName => this.displayAttribute != null ? this.displayAttribute.GetShortName() : this.enumValue.ToString();
}
}
I tried doing this as an edit but it was rejected; I can't see why.
The above will throw an exception if you call it with an Enum that has a mix of custom attributes and plain items, e.g.
public enum CommentType
{
All = 1,
Rent = 2,
Insurance = 3,
[Display(Name="Service Charge")]
ServiceCharge = 4
}
So I've modified the code ever so slightly to check for custom attributes before trying to access them, and use the name if none are found.
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Reflection;
public static class EnumHelper<T>
{
public static IList<T> GetValues(Enum value)
{
var enumValues = new List<T>();
foreach (FieldInfo fi in value.GetType().GetFields(BindingFlags.Static | BindingFlags.Public))
{
enumValues.Add((T)Enum.Parse(value.GetType(), fi.Name, false));
}
return enumValues;
}
public static T Parse(string value)
{
return (T)Enum.Parse(typeof(T), value, true);
}
public static IList<string> GetNames(Enum value)
{
return value.GetType().GetFields(BindingFlags.Static | BindingFlags.Public).Select(fi => fi.Name).ToList();
}
public static IList<string> GetDisplayValues(Enum value)
{
return GetNames(value).Select(obj => GetDisplayValue(Parse(obj))).ToList();
}
private static string lookupResource(Type resourceManagerProvider, string resourceKey)
{
foreach (PropertyInfo staticProperty in resourceManagerProvider.GetProperties(BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public))
{
if (staticProperty.PropertyType == typeof(System.Resources.ResourceManager))
{
System.Resources.ResourceManager resourceManager = (System.Resources.ResourceManager)staticProperty.GetValue(null, null);
return resourceManager.GetString(resourceKey);
}
}
return resourceKey; // Fallback with the key name
}
public static string GetDisplayValue(T value)
{
var fieldInfo = value.GetType().GetField(value.ToString());
var descriptionAttributes = fieldInfo.GetCustomAttributes(
typeof(DisplayAttribute), false) as DisplayAttribute[];
if (descriptionAttributes.Any() && descriptionAttributes[0].ResourceType != null)
return lookupResource(descriptionAttributes[0].ResourceType, descriptionAttributes[0].Name);
if (descriptionAttributes == null) return string.Empty;
return (descriptionAttributes.Length > 0) ? descriptionAttributes[0].Name : value.ToString();
}
}
Using MVC5 you could use:
public enum UserPromotion
{
None = 0x0,
[Display(Name = "Send Job Offers By Mail")]
SendJobOffersByMail = 0x1,
[Display(Name = "Send Job Offers By Sms")]
SendJobOffersBySms = 0x2,
[Display(Name = "Send Other Stuff By Sms")]
SendPromotionalBySms = 0x4,
[Display(Name = "Send Other Stuff By Mail")]
SendPromotionalByMail = 0x8
}
then if you want to create a dropdown selector you can use:
#Html.EnumDropdownListFor(expression: model => model.PromotionSelector, optionLabel: "Select")
assume that your enum name is OrderState, Use this code:
#Html.DropDownList("selectList", new SelectList(Html.GetEnumSelectList<OrderState>(), "Value", "Text",ViewBag.selectedOrderState), new {#id="OrderState", #class = "form-control" })
and set selected option in backend:
var selectedOrderState = ..Data.OrderState.GetHashCode();
ViewBag.selectedOrderState = selectedOrderState;
Related
How to get a custom attribute on enum by passing in the enum value and attribute type?
there are lots of examples online of creating a enum extension method that takes a enum value as an argument and in the method gets a specific attribute, like so: namespace MVVMProj.ProjUtilities { public class EnumerationHelpers { public static string GetStatusText(this Enum value) { var type = value.GetType(); string name = Enum.GetName(type, value); if (name == null) { return null; } var field = type.GetField(name); if (field == null) { return null; } var attr = Attribute.GetCustomAttribute(field, typeof(StatusTextAttribute)) as StatusTextAttribute; if (attr == null) { return null; } return attr.StatusText; } } } What I'm wondering is, is there a way to also pass the method the attribute type, so I don't need to keep writing specific methods for each different attribute? This is unfinished, but, it should give you the idea of what I'm trying to achieve: namespace MVVMProj.ProjUtilities { public class EnumerationHelpers { public static string GetCustomAttribute(this Enum value, Type customAttr) //Or instead of passing a Type, a string of the attribute's name { var type = value.GetType(); string name = Enum.GetName(type, value); if (name == null) { return null; } var field = type.GetField(name); if (field == null) { return null; } var attr = Attribute.GetCustomAttribute(field, ....) as ....; if (attr == null) { return null; } return attr....; } } } I suppose I can't just return a string either as it could be any data type. Some generic method maybe? Any advice would be greatly appreciated! Edit: Usage: It is iterating over the enum creating a dictionary so I can display the values in a combobox. It only adds the item if the attribute matches the condition in the if statement. One more thing to note is that the custom attribute is an enum as well. Aybe: the 'item' is only an object upon the iteration so I do a cast. Though I am getting an error in the if statement, it trying to compare CaseTypeAttribute to an actual CaseType enum value, what do I need to do to resolve? Error: Severity Code Description Project File Line Suppression State Error CS0019 Operator '==' cannot be applied to operands of type 'SBC.CaseTypeAttribute' and 'SBC.CaseType' private Dictionary<int, string> _substancetypes; public Dictionary<int, string> SubstanceTypes { get { if (_substancetypes == null) { _substancetypes = new Dictionary<int, string>(); foreach (var item in Enum.GetValues(typeof(SBC.SubstanceTypeCode))) { var descriptionAttribute = ((SBC.SubstanceTypeCode)item).GetAttribute<SBC.CaseTypeAttribute>(); if (descriptionAttribute != null && descriptionAttribute == SBC.CaseType.Exposures) //Error here { _substancetypes.Add((int)item, CTS_MVVM.CTS_Utilities.EnumerationHelpers.GetDescriptionFromEnumValue((SBC.SubstanceTypeCode)item)); } } } return _substancetypes; } }
Something like this? using System; using System.ComponentModel; using System.Reflection; namespace ConsoleApp1 { internal static class Program { private static void Main(string[] args) { var descriptionAttribute = MyEnum.A.GetAttribute<DescriptionAttribute>(); } } public static class EnumExtensions { public static T GetAttribute<T>(this Enum #enum) where T : Attribute { var type = #enum.GetType(); var name = Enum.GetName(type, #enum); var field = type.GetField(name); var attribute = field.GetCustomAttribute<T>(); return attribute; } } public enum MyEnum { [Description("A")] A, [Description("B")] B } }
About Enum and DataAnnotation
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?
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)]
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")
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."); } } }
Need Enum's Display Names as a List [duplicate]
I've got a property in my model called Promotion that its type is a flag enum called UserPromotion. Members of my enum have display attributes set as follows: [Flags] public enum UserPromotion { None = 0x0, [Display(Name = "Send Job Offers By Mail")] SendJobOffersByMail = 0x1, [Display(Name = "Send Job Offers By Sms")] SendJobOffersBySms = 0x2, [Display(Name = "Send Other Stuff By Sms")] SendPromotionalBySms = 0x4, [Display(Name = "Send Other Stuff By Mail")] SendPromotionalByMail = 0x8 } Now I want to be able to create say a ul in my view to show the selected values of my Promotion property. This is what I have done so far but the problem is that how can I get the display names here? <ul> #foreach (int aPromotion in #Enum.GetValues(typeof(UserPromotion))) { var currentPromotion = (int)Model.JobSeeker.Promotion; if ((currentPromotion & aPromotion) == aPromotion) { <li>Here I don't know how to get the display attribute of "currentPromotion".</li> } } </ul>
One liner - Fluent syntax public static class Extensions { /// <summary> /// A generic extension method that aids in reflecting /// and retrieving any attribute that is applied to an `Enum`. /// </summary> public static TAttribute GetAttribute<TAttribute>(this Enum enumValue) where TAttribute : Attribute { return enumValue.GetType() .GetMember(enumValue.ToString()) .First() .GetCustomAttribute<TAttribute>(); } } Example public enum Season { [Display(Name = "It's autumn")] Autumn, [Display(Name = "It's winter")] Winter, [Display(Name = "It's spring")] Spring, [Display(Name = "It's summer")] Summer } public class Foo { public Season Season = Season.Summer; public void DisplayName() { var seasonDisplayName = Season.GetAttribute<DisplayAttribute>(); Console.WriteLine("Which season is it?"); Console.WriteLine (seasonDisplayName.Name); } } Output Which season is it? It's summer
UPDATE First solution was focused on getting display names from enum. Code below should be exact solution for your problem. You can use this helper class for enums: using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Reflection; public static class EnumHelper<T> where T : struct, Enum // This constraint requires C# 7.3 or later. { public static IList<T> GetValues(Enum value) { var enumValues = new List<T>(); foreach (FieldInfo fi in value.GetType().GetFields(BindingFlags.Static | BindingFlags.Public)) { enumValues.Add((T)Enum.Parse(value.GetType(), fi.Name, false)); } return enumValues; } public static T Parse(string value) { return (T)Enum.Parse(typeof(T), value, true); } public static IList<string> GetNames(Enum value) { return value.GetType().GetFields(BindingFlags.Static | BindingFlags.Public).Select(fi => fi.Name).ToList(); } public static IList<string> GetDisplayValues(Enum value) { return GetNames(value).Select(obj => GetDisplayValue(Parse(obj))).ToList(); } private static string lookupResource(Type resourceManagerProvider, string resourceKey) { var resourceKeyProperty = resourceManagerProvider.GetProperty(resourceKey, BindingFlags.Static | BindingFlags.Public, null, typeof(string), new Type[0], null); if (resourceKeyProperty != null) { return (string)resourceKeyProperty.GetMethod.Invoke(null, null); } return resourceKey; // Fallback with the key name } public static string GetDisplayValue(T value) { var fieldInfo = value.GetType().GetField(value.ToString()); var descriptionAttributes = fieldInfo.GetCustomAttributes( typeof(DisplayAttribute), false) as DisplayAttribute[]; if (descriptionAttributes[0].ResourceType != null) return lookupResource(descriptionAttributes[0].ResourceType, descriptionAttributes[0].Name); if (descriptionAttributes == null) return string.Empty; return (descriptionAttributes.Length > 0) ? descriptionAttributes[0].Name : value.ToString(); } } And then you can use it in your view as following: <ul> #foreach (var value in #EnumHelper<UserPromotion>.GetValues(UserPromotion.None)) { if (value == Model.JobSeeker.Promotion) { var description = EnumHelper<UserPromotion>.GetDisplayValue(value); <li>#Html.DisplayFor(e => description )</li> } } </ul>
Building on Aydin's great answer, here's an extension method that doesn't require any type parameters. using System; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Reflection; public static class EnumExtensions { public static string GetDisplayName(this Enum enumValue) { return enumValue.GetType() .GetMember(enumValue.ToString()) .First() .GetCustomAttribute<DisplayAttribute>() .GetName(); } } NOTE: GetName() should be used instead of the Name property. This ensures that the localized string will be returned if using the ResourceType attribute property. Example To use it, just reference the enum value in your view. #{ UserPromotion promo = UserPromotion.SendJobOffersByMail; } Promotion: #promo.GetDisplayName() Output Promotion: Send Job Offers By Mail
Based on Aydin's answer I would suggest a less "duplicatious" implementation (because we could easily get the Type from the Enum value itself, instead of providing it as a parameter 馃槈: using System; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Reflection; public static string GetDisplayName(this Enum enumValue) { return enumValue.GetType().GetMember(enumValue.ToString()) .First() .GetCustomAttribute<DisplayAttribute>() .Name; } EDIT (based upon #Vahagn Nahapetyan's comment) public static string GetDisplayName(this Enum enumValue) { return enumValue.GetType()? .GetMember(enumValue.ToString())? .First()? .GetCustomAttribute<DisplayAttribute>()? .Name; } Now we can use it very clean in this way: public enum Season { [Display(Name = "The Autumn")] Autumn, [Display(Name = "The Weather")] Winter, [Display(Name = "The Tease")] Spring, [Display(Name = "The Dream")] Summer } Season.Summer.GetDisplayName(); Which results in "The Dream"
If you are using MVC 5.1 or upper there is simplier and clearer way: just use data annotation (from System.ComponentModel.DataAnnotations namespace) like below: public enum Color { [Display(Name = "Dark red")] DarkRed, [Display(Name = "Very dark red")] VeryDarkRed, [Display(Name = "Red or just black?")] ReallyDarkRed } And in view, just put it into proper html helper: #Html.EnumDropDownListFor(model => model.Color)
Building on Todd's great answer which built on Aydin's great answer, here's a generic extension method which doesn't require any type parameters. /// <summary> /// Gets human-readable version of enum. /// </summary> /// <returns>effective DisplayAttribute.Name of given enum.</returns> public static string GetDisplayName<T>(this T enumValue) where T : IComparable, IFormattable, IConvertible // C# 7.3+: where T : struct, Enum { if (!typeof(T).IsEnum) // Not needed in C# 7.3+ with above updated constraint throw new ArgumentException("Argument must be of type Enum"); DisplayAttribute displayAttribute = enumValue.GetType() .GetMember(enumValue.ToString()) .First() .GetCustomAttribute<DisplayAttribute>(); string displayName = displayAttribute?.GetName(); return displayName ?? enumValue.ToString(); } I needed this for my project because something like the below code, where not every member of the enum has a DisplayAttribute, throws an exception with Todd's solution: public class MyClass { public enum MyEnum { [Display(Name="ONE")] One, // No DisplayAttribute Two } public void UseMyEnum() { MyEnum foo = MyEnum.One; MyEnum bar = MyEnum.Two; Console.WriteLine(foo.GetDisplayName()); Console.WriteLine(bar.GetDisplayName()); } } // Output: // // ONE // Two If this is a complicated solution to a simple problem, please let me know, but this was the fix I used.
You could use Type.GetMember Method, then get the attribute info using reflection: // display attribute of "currentPromotion" var type = typeof(UserPromotion); var memberInfo = type.GetMember(currentPromotion.ToString()); var attributes = memberInfo[0].GetCustomAttributes(typeof(DisplayAttribute), false); var description = ((DisplayAttribute)attributes[0]).Name; There were a few similar posts here: Getting attributes of Enum's value How to make MVC3 DisplayFor show the value of an Enum's Display-Attribute?
In .NET5, I used DisplayTextFor without needing helper or extension methods: #Html.DisplayTextFor(m => m.SomeEnumProperty) Where SomeEnumProperty has a value of: public enum MyEnum { [Display(Name = "Not started")] NotStarted = 0, [Display(Name = "Weird display name instead of just 'Started'")] Started = 1, }
For ASP.Net Core 3.0, this worked for me (credit to previous answerers). My Enum class: using System; using System.Linq; using System.ComponentModel.DataAnnotations; using System.Reflection; public class Enums { public enum Duration { [Display(Name = "1 Hour")] OneHour, [Display(Name = "1 Day")] OneDay } // Helper method to display the name of the enum values. public static string GetDisplayName(Enum value) { return value.GetType()? .GetMember(value.ToString())?.First()? .GetCustomAttribute<DisplayAttribute>()? .Name; } } My View Model Class: public class MyViewModel { public Duration Duration { get; set; } } An example of a razor view displaying a label and a drop-down list. Notice the drop-down list does not require a helper method: #model IEnumerable<MyViewModel> #foreach (var item in Model) { <label asp-for="#item.Duration">#Enums.GetDisplayName(item.Duration)</label> <div class="form-group"> <label asp-for="#item.Duration" class="control-label">Select Duration</label> <select asp-for="#item.Duration" class="form-control" asp-items="Html.GetEnumSelectList<Enums.Duration>()"> </select> </div> }
With Core 2.1, public static string GetDisplayName(Enum enumValue) { return enumValue.GetType()? .GetMember(enumValue.ToString())?[0]? .GetCustomAttribute<DisplayAttribute>()? .Name; }
<ul> #foreach (int aPromotion in #Enum.GetValues(typeof(UserPromotion))) { var currentPromotion = (int)Model.JobSeeker.Promotion; if ((currentPromotion & aPromotion) == aPromotion) { <li>#Html.DisplayFor(e => currentPromotion)</li> } } </ul>
combining all edge-cases together from above: enum members with base object members' names (Equals, ToString) optional Display attribute here is my code: public enum Enum { [Display(Name = "What a weird name!")] ToString, Equals } public static class EnumHelpers { public static string GetDisplayName(this Enum enumValue) { var enumType = enumValue.GetType(); return enumType .GetMember(enumValue.ToString()) .Where(x => x.MemberType == MemberTypes.Field && ((FieldInfo)x).FieldType == enumType) .First() .GetCustomAttribute<DisplayAttribute>()?.Name ?? enumValue.ToString(); } } void Main() { Assert.Equals("What a weird name!", Enum.ToString.GetDisplayName()); Assert.Equals("Equals", Enum.Equals.GetDisplayName()); }
You need to use a bit of reflection in order to access that attribute: var type = typeof(UserPromotion); var member = type.GetMember(Model.JobSeeker.Promotion.ToString()); var attributes = member[0].GetCustomAttributes(typeof(DisplayAttribute), false); var name = ((DisplayAttribute)attributes[0]).Name; I recommend wrapping this method in a extension method or perform this in a view model.
I'm sorry to do this, but I couldn't use any of the other answers as-is and haven't time to duke it out in the comments. Uses C# 6 syntax. static class EnumExtensions { /// returns the localized Name, if a [Display(Name="Localised Name")] attribute is applied to the enum member /// returns null if there isnt an attribute public static string DisplayNameOrEnumName(this Enum value) // => value.DisplayNameOrDefault() ?? value.ToString() { // More efficient form of ^ based on http://stackoverflow.com/a/17034624/11635 var enumType = value.GetType(); var enumMemberName = Enum.GetName(enumType, value); return enumType .GetEnumMemberAttribute<DisplayAttribute>(enumMemberName) ?.GetName() // Potentially localized ?? enumMemberName; // Or fall back to the enum name } /// returns the localized Name, if a [Display] attribute is applied to the enum member /// returns null if there is no attribute public static string DisplayNameOrDefault(this Enum value) => value.GetEnumMemberAttribute<DisplayAttribute>()?.GetName(); static TAttribute GetEnumMemberAttribute<TAttribute>(this Enum value) where TAttribute : Attribute => value.GetType().GetEnumMemberAttribute<TAttribute>(value.ToString()); static TAttribute GetEnumMemberAttribute<TAttribute>(this Type enumType, string enumMemberName) where TAttribute : Attribute => enumType.GetMember(enumMemberName).Single().GetCustomAttribute<TAttribute>(); }
Building further on Aydin's and Todd's answers, here is an extension method that also lets you get the name from a resource file using AppResources; using System; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Reflection; using System.Resources; public static class EnumExtensions { public static string GetDisplayName(this Enum enumValue) { var enumMember= enumValue.GetType() .GetMember(enumValue.ToString()); DisplayAttribute displayAttrib = null; if (enumMember.Any()) { displayAttrib = enumMember .First() .GetCustomAttribute<DisplayAttribute>(); } string name = null; Type resource = null; if (displayAttrib != null) { name = displayAttrib.Name; resource = displayAttrib.ResourceType; } return String.IsNullOrEmpty(name) ? enumValue.ToString() : resource == null ? name : new ResourceManager(resource).GetString(name); } } and use it like public enum Season { [Display(ResourceType = typeof(Resource), Name = Season_Summer")] Summer }
For just displaying enum's display name attribute just use Microsoft.AspNetCore.Mvc.Rendering's #Html.DisplayFor(x => EnumType.EnumValue) That's would be enough. For displaying SelectList write as following: <select id="someIdForTheEndPoint" asp-items="Html.GetEnumSelectList<EnumType>()"> <option selected="selected" value="">Select value</option> </select>
I have two solutions for this Question. The first solution is on getting display names from enum. public enum CourseLocationTypes { [Display(Name = "On Campus")] OnCampus, [Display(Name = "Online")] Online, [Display(Name = "Both")] Both } public static string DisplayName(this Enum value) { Type enumType = value.GetType(); string enumValue = Enum.GetName(enumType, value); MemberInfo member = enumType.GetMember(enumValue)[0]; object[] attrs = member.GetCustomAttributes(typeof(DisplayAttribute), false); string outString = ((DisplayAttribute)attrs[0]).Name; if (((DisplayAttribute)attrs[0]).ResourceType != null) { outString = ((DisplayAttribute)attrs[0]).GetName(); } return outString; } <h3 class="product-title white">#Model.CourseLocationType.DisplayName()</h3> The second Solution is on getting display name from enum name but that will be enum split in developer language it's called patch. public static string SplitOnCapitals(this string text) { var r = new Regex(#" (?<=[A-Z])(?=[A-Z][a-z]) | (?<=[^A-Z])(?=[A-Z]) | (?<=[A-Za-z])(?=[^A-Za-z])", RegexOptions.IgnorePatternWhitespace); return r.Replace(text, " "); } <div class="widget-box pt-0"> #foreach (var item in Enum.GetNames(typeof(CourseLocationType))) { <label class="pr-2 pt-1"> #Html.RadioButtonFor(x => x.CourseLocationType, item, new { type = "radio", #class = "iCheckBox control-label" }) #item.SplitOnCapitals() </label> } #Html.ValidationMessageFor(x => x.CourseLocationType) </div>
2020 Update: An updated version of the function provided by many in this thread but now for C# 7.3 onwards: Now you can restrict generic methods to enums types so you can write a single method extension to use it with all your enums like this: The generic extension method: public static string ATexto<T>(this T enumeraci贸n) where T : struct, Enum { var tipo = enumeraci贸n.GetType(); return tipo.GetMember(enumeraci贸n.ToString()) .Where(x => x.MemberType == MemberTypes.Field && ((FieldInfo)x).FieldType == tipo).First() .GetCustomAttribute<DisplayAttribute>()?.Name ?? enumeraci贸n.ToString(); } The enum: public enum TipoImpuesto { IVA, INC, [Display(Name = "IVA e INC")]IVAeINC, [Display(Name = "No aplica")]NoAplica }; How to use it: var tipoImpuesto = TipoImpuesto.IVAeINC; var textoTipoImpuesto = tipoImpuesto.ATexto(); // Prints "IVA e INC". Bonus, Enums with Flags: If you are dealing with normal enums the function above is enough, but if any of your enums can take multiple values with the use of flags then you will need to modify it like this (This code uses C#8 features): public static string ATexto<T>(this T enumeraci贸n) where T : struct, Enum { var tipo = enumeraci贸n.GetType(); var textoDirecto = enumeraci贸n.ToString(); string obtenerTexto(string textoDirecto) => tipo.GetMember(textoDirecto) .Where(x => x.MemberType == MemberTypes.Field && ((FieldInfo)x).FieldType == tipo) .First().GetCustomAttribute<DisplayAttribute>()?.Name ?? textoDirecto; if (textoDirecto.Contains(", ")) { var texto = new StringBuilder(); foreach (var textoDirectoAux in textoDirecto.Split(", ")) { texto.Append($"{obtenerTexto(textoDirectoAux)}, "); } return texto.ToString()[0..^2]; } else { return obtenerTexto(textoDirecto); } } The enum with flags: [Flags] public enum TipoContribuyente { [Display(Name = "Com煤n")] Com煤n = 1, [Display(Name = "Gran Contribuyente")] GranContribuyente = 2, Autorretenedor = 4, [Display(Name = "Retenedor de IVA")] RetenedorIVA = 8, [Display(Name = "R茅gimen Simple")] R茅gimenSimple = 16 } How to use it: var tipoContribuyente = TipoContribuyente.RetenedorIVA | TipoContribuyente.GranContribuyente; var textoAux = tipoContribuyente.ATexto(); // Prints "Gran Contribuyente, Retenedor de IVA".
I want to contribute with culture-dependent GetDisplayName enum extension. Hope this will be usefull for anyone googling this answer like me previously: "standart" way as Aydin Adn and Todd mentioned: public static string GetDisplayName(this Enum enumValue) { return enumValue .GetType() .GetMember(enumValue.ToString()) .First() .GetCustomAttribute<DisplayAttribute>() .GetName(); } "Culture-dependent" way: public static string GetDisplayName(this Enum enumValue, CultureInfo ci) { var displayAttr = enumValue .GetType() .GetMember(enumValue.ToString()) .First() .GetCustomAttribute<DisplayAttribute>(); var resMan = displayAttr.ResourceType?.GetProperty(#"ResourceManager", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic).GetValue(null, null) as ResourceManager; return resMan?.GetString(displayAttr.Name, ci) ?? displayAttr.GetName(); }
It is maybe cheating, but it's works: #foreach (var yourEnum in Html.GetEnumSelectList<YourEnum>()) { #yourEnum.Text }
Based on previous answers I've created this comfortable helper to support all DisplayAttribute properties in a readable way: public static class EnumExtensions { public static DisplayAttributeValues GetDisplayAttributeValues(this Enum enumValue) { var displayAttribute = enumValue.GetType().GetMember(enumValue.ToString()).First().GetCustomAttribute<DisplayAttribute>(); return new DisplayAttributeValues(enumValue, displayAttribute); } public sealed class DisplayAttributeValues { private readonly Enum enumValue; private readonly DisplayAttribute displayAttribute; public DisplayAttributeValues(Enum enumValue, DisplayAttribute displayAttribute) { this.enumValue = enumValue; this.displayAttribute = displayAttribute; } public bool? AutoGenerateField => this.displayAttribute?.GetAutoGenerateField(); public bool? AutoGenerateFilter => this.displayAttribute?.GetAutoGenerateFilter(); public int? Order => this.displayAttribute?.GetOrder(); public string Description => this.displayAttribute != null ? this.displayAttribute.GetDescription() : string.Empty; public string GroupName => this.displayAttribute != null ? this.displayAttribute.GetGroupName() : string.Empty; public string Name => this.displayAttribute != null ? this.displayAttribute.GetName() : this.enumValue.ToString(); public string Prompt => this.displayAttribute != null ? this.displayAttribute.GetPrompt() : string.Empty; public string ShortName => this.displayAttribute != null ? this.displayAttribute.GetShortName() : this.enumValue.ToString(); } }
I tried doing this as an edit but it was rejected; I can't see why. The above will throw an exception if you call it with an Enum that has a mix of custom attributes and plain items, e.g. public enum CommentType { All = 1, Rent = 2, Insurance = 3, [Display(Name="Service Charge")] ServiceCharge = 4 } So I've modified the code ever so slightly to check for custom attributes before trying to access them, and use the name if none are found. using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Reflection; public static class EnumHelper<T> { public static IList<T> GetValues(Enum value) { var enumValues = new List<T>(); foreach (FieldInfo fi in value.GetType().GetFields(BindingFlags.Static | BindingFlags.Public)) { enumValues.Add((T)Enum.Parse(value.GetType(), fi.Name, false)); } return enumValues; } public static T Parse(string value) { return (T)Enum.Parse(typeof(T), value, true); } public static IList<string> GetNames(Enum value) { return value.GetType().GetFields(BindingFlags.Static | BindingFlags.Public).Select(fi => fi.Name).ToList(); } public static IList<string> GetDisplayValues(Enum value) { return GetNames(value).Select(obj => GetDisplayValue(Parse(obj))).ToList(); } private static string lookupResource(Type resourceManagerProvider, string resourceKey) { foreach (PropertyInfo staticProperty in resourceManagerProvider.GetProperties(BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public)) { if (staticProperty.PropertyType == typeof(System.Resources.ResourceManager)) { System.Resources.ResourceManager resourceManager = (System.Resources.ResourceManager)staticProperty.GetValue(null, null); return resourceManager.GetString(resourceKey); } } return resourceKey; // Fallback with the key name } public static string GetDisplayValue(T value) { var fieldInfo = value.GetType().GetField(value.ToString()); var descriptionAttributes = fieldInfo.GetCustomAttributes( typeof(DisplayAttribute), false) as DisplayAttribute[]; if (descriptionAttributes.Any() && descriptionAttributes[0].ResourceType != null) return lookupResource(descriptionAttributes[0].ResourceType, descriptionAttributes[0].Name); if (descriptionAttributes == null) return string.Empty; return (descriptionAttributes.Length > 0) ? descriptionAttributes[0].Name : value.ToString(); } }
Using MVC5 you could use: public enum UserPromotion { None = 0x0, [Display(Name = "Send Job Offers By Mail")] SendJobOffersByMail = 0x1, [Display(Name = "Send Job Offers By Sms")] SendJobOffersBySms = 0x2, [Display(Name = "Send Other Stuff By Sms")] SendPromotionalBySms = 0x4, [Display(Name = "Send Other Stuff By Mail")] SendPromotionalByMail = 0x8 } then if you want to create a dropdown selector you can use: #Html.EnumDropdownListFor(expression: model => model.PromotionSelector, optionLabel: "Select")
assume that your enum name is OrderState, Use this code: #Html.DropDownList("selectList", new SelectList(Html.GetEnumSelectList<OrderState>(), "Value", "Text",ViewBag.selectedOrderState), new {#id="OrderState", #class = "form-control" }) and set selected option in backend: var selectedOrderState = ..Data.OrderState.GetHashCode(); ViewBag.selectedOrderState = selectedOrderState;
enum values in drop down list using ViewData and th viewcode to list it?
How can I create a dropdown list using an enum value in ASP.NET MVC 4? I have a Language enumeration: public enum Language { English = 0, spanish = 2, Arabi = 3 } And my property is: public Language Language { get; set; } And my Controller action looks like this: [HttpPost] public ActionResult Edit(tList tableSheet) { return RedirectToAction("Index"); } How will I call in my view through a dropdown list using ViewData[]?
This will return Enum.GetNames(typeOf(Language )) English spanish Arabi And this Enum.GetValues(typeOf(Language )) 1,2,3 You can languages list to view: ViewBeg.Languages = Enum.GetNames(typeOf(Language)).ToList();
I know i'm late to the party but... check out a helper class I created to do just this... http://jnye.co/Posts/4/creating-a-dropdown-list-from-an-enum-in-mvc-and-c%23 This helpe can be used as follows: In the controller: //If you don't have an enum value use the type ViewBag.DropDownList = EnumHelper.SelectListFor<Language>(); //If you do have an enum value use the value (the value will be marked as selected) ViewBag.DropDownList = EnumHelper.SelectListFor(myEnumValue); In the view #Html.DropDownList("DropDownList") Helper: public static class EnumHelper { //Creates a SelectList for a nullable enum value public static SelectList SelectListFor<T>(T? selected) where T : struct { return selected == null ? SelectListFor<T>() : SelectListFor(selected.Value); } //Creates a SelectList for an enum type public static SelectList SelectListFor<T>() where T : struct { Type t = typeof (T); if (t.IsEnum) { var values = Enum.GetValues(typeof(T)).Cast<enum>() .Select(e => new { Id = Convert.ToInt32(e), Name = e.GetDescription() }); return new SelectList(values, "Id", "Name"); } return null; } //Creates a SelectList for an enum value public static SelectList SelectListFor<T>(T selected) where T : struct { Type t = typeof(T); if (t.IsEnum) { var values = Enum.GetValues(t).Cast<Enum>() .Select(e => new { Id = Convert.ToInt32(e), Name = e.GetDescription() }); return new SelectList(values, "Id", "Name", Convert.ToInt32(selected)); } return null; } // Get the value of the description attribute if the // enum has one, otherwise use the value. public static string GetDescription<TEnum>(this TEnum value) { FieldInfo fi = value.GetType().GetField(value.ToString()); if (fi != null) { DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes( typeof(DescriptionAttribute), false); if (attributes.Length > 0) { return attributes[0].Description; } } return value.ToString(); } }
Custom attribute on property - Getting type and value of attributed property
I have the following custom attribute, which can be applied on properties: [AttributeUsage(AttributeTargets.Property, AllowMultiple = false)] public class IdentifierAttribute : Attribute { } For example: public class MyClass { [Identifier()] public string Name { get; set; } public int SomeNumber { get; set; } public string SomeOtherProperty { get; set; } } There will also be other classes, to which the Identifier attribute could be added to properties of different type: public class MyOtherClass { public string Name { get; set; } [Identifier()] public int SomeNumber { get; set; } public string SomeOtherProperty { get; set; } } I then need to be able to get this information in my consuming class. For example: public class TestClass<T> { public void GetIDForPassedInObject(T obj) { var type = obj.GetType(); //type.GetCustomAttributes(true)??? } } What's the best way of going about this? I need to get the type of the [Identifier()] field (int, string, etc...) and the actual value, obviously based on the type.
Something like the following,, this will use only the first property it comes accross that has the attribute, of course you could place it on more than one.. public object GetIDForPassedInObject(T obj) { var prop = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance) .FirstOrDefault(p => p.GetCustomAttributes(typeof(IdentifierAttribute), false).Count() ==1); object ret = prop !=null ? prop.GetValue(obj, null) : null; return ret; }
public class TestClass<T> { public void GetIDForPassedInObject(T obj) { PropertyInfo[] properties = obj.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance); PropertyInfo IdProperty = (from PropertyInfo property in properties where property.GetCustomAttributes(typeof(Identifier), true).Length > 0 select property).First(); if(null == IdProperty) throw new ArgumentException("obj does not have Identifier."); Object propValue = IdProperty.GetValue(entity, null) } }
A bit late but here is something I did for enums (could be any object also) and getting the description attribute value using an extension (this could be a generic for any attribute): public enum TransactionTypeEnum { [Description("Text here!")] DROP = 1, [Description("More text here!")] PICKUP = 2, ... } Getting the value: var code = TransactionTypeEnum.DROP.ToCode(); Extension supporting all my enums: public static string ToCode(this TransactionTypeEnum val) { return GetCode(val); } public static string ToCode(this DockStatusEnum val) { return GetCode(val); } public static string ToCode(this TrailerStatusEnum val) { return GetCode(val); } public static string ToCode(this DockTrailerStatusEnum val) { return GetCode(val); } public static string ToCode(this EncodingType val) { return GetCode(val); } private static string GetCode(object val) { var attributes = (DescriptionAttribute[])val.GetType().GetField(val.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), false); return attributes.Length > 0 ? attributes[0].Description : string.Empty; }
Here is a more real-word example. We use an extension method and check if a property contains a FieldMetaDataAttribute (a custom attribute in my source code base) with valid Major and MinorVersion. What is of general interest is the part where we use the parent class type and GetProperties and retrieve the ProperyInfo and then use GetCustomAttribute to retrieve a attribute FieldMetaDataAttribute in this special case. Use this code for inspiration how to do more generic way of retrieving a custom attribute. This can of course be polished to make a general method to retrieve a given attribute of any property of a class instance. /// <summary> /// Executes the action if not the field is deprecated /// </summary> /// <typeparam name="TProperty"></typeparam> /// <typeparam name="TForm"></typeparam> /// <param name="form"></param> /// <param name="memberExpression"></param> /// <param name="actionToPerform"></param> /// <returns>True if the action was performed</returns> public static bool ExecuteActionIfNotDeprecated<TForm, TProperty>(this TForm form, Expression<Func<TForm, TProperty>> memberExpression, Action actionToPerform) { var memberExpressionConverted = memberExpression.Body as MemberExpression; if (memberExpressionConverted == null) return false; string memberName = memberExpressionConverted.Member.Name; PropertyInfo matchingProperty = typeof(TForm).GetProperties(BindingFlags.Public | BindingFlags.Instance) .FirstOrDefault(p => p.Name == memberName); if (matchingProperty == null) return false; //should not occur var fieldMeta = matchingProperty.GetCustomAttribute(typeof(FieldMetadataAttribute), true) as FieldMetadataAttribute; if (fieldMeta == null) { actionToPerform(); return true; } var formConverted = form as FormDataContract; if (formConverted == null) return false; if (fieldMeta.DeprecatedFromMajorVersion > 0 && formConverted.MajorVersion > fieldMeta.DeprecatedFromMajorVersion) { //major version of formConverted is deprecated for this field - do not execute action return false; } if (fieldMeta.DeprecatedFromMinorVersion > 0 && fieldMeta.DeprecatedFromMajorVersion > 0 && formConverted.MinorVersion >= fieldMeta.DeprecatedFromMinorVersion && formConverted.MajorVersion >= fieldMeta.DeprecatedFromMajorVersion) return false; //the field is expired - do not invoke action actionToPerform(); return true; }