How to get the Display Name Attribute of an Enum member via MVC Razor code?

How to Get the Display Name Attribute of an Enum Member via MVC Razor Code? 😎💻
Have you ever found yourself in a situation where you needed to display the display name attribute of an enum member in your MVC Razor code? 🤔 Fear not! In this blog post, we'll dive into a common issue faced by developers and provide you with easy solutions to retrieve and display the enum member's display name attribute. 💪
The Problem: Getting the Display Names of Enum Members 🎭
Imagine you have a model property called Promotion, and its type is a flag enum named UserPromotion. Each member of the UserPromotion enum has a display attribute assigned to it, which specifies how the member should be displayed.
[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, let's say you want to create a <ul> in your Razor view to show the selected values of the Promotion property. The challenge is figuring out how to retrieve and display the display names of each selected UserPromotion enum member.
Solution: Using MVC Razor Code to Get Display Names 🌟
To solve this problem, we can utilize the powerful MVC Razor code to access the display name attribute of each enum member. Here's how you can do it step-by-step:
Inside your Razor view, create a
<ul>element to hold the list of selected values.
<ul>
@foreach (int aPromotion in Enum.GetValues(typeof(UserPromotion)))
{
var currentPromotion = (int)Model.JobSeeker.Promotion;
if ((currentPromotion & aPromotion) == aPromotion)
{
// Display the display name attribute of the current promotion
}
}
</ul>To get the display name attribute of the current
UserPromotionenum member, you can use theDisplayAttributeclass provided by .NET.
<ul>
@foreach (int aPromotion in Enum.GetValues(typeof(UserPromotion)))
{
var currentPromotion = (int)Model.JobSeeker.Promotion;
if ((currentPromotion & aPromotion) == aPromotion)
{
var displayAttribute = typeof(UserPromotion).GetMember(Enum.GetName(typeof(UserPromotion), aPromotion))
.FirstOrDefault()?.GetCustomAttributes(typeof(DisplayAttribute), false)
.FirstOrDefault() as DisplayAttribute;
<li>@displayAttribute?.Name</li>
}
}
</ul>By using the GetMember() method in conjunction with GetCustomAttributes(), we can access the display attribute of the current enum member and retrieve its display name.
Finally, we can display the enum member's display name by using the
@displayAttribute?.Namesyntax in Razor.
Conclusion and Call-to-Action 🏁🙌
Congratulations! You've learned how to retrieve and display the display name attribute of an enum member in your MVC Razor code. Now you can easily create a list of selected enum values that are user-friendly and meaningful to your users. 🎉
If you found this blog post helpful, make sure to share it with your developer friends who might benefit from it. And don't forget to subscribe to our newsletter for more helpful tech tips and tricks! 💌👨💻
Take Your Tech Career to the Next Level
Our application tracking tool helps you manage your job search effectively. Stay organized, track your progress, and land your dream tech job faster.



