Home > Backend Development > C++ > How to Retrieve Enum Display Names in ASP.NET MVC Razor Views?

How to Retrieve Enum Display Names in ASP.NET MVC Razor Views?

Linda Hamilton
Release: 2025-01-27 09:26:12
Original
818 people have browsed it

How to Retrieve Enum Display Names in ASP.NET MVC Razor Views?

The name of the name

in the MVC Razor code

In the ASP.NET MVC Razor view, the display name of accessing enumeration members is essential to describe a friendly description to users. This article discusses how to retrieve these display names in the context of the MVC Razor code.

Problem description

Given a lift member who modifies [Display] attributes, challenges to extract these display names in the Razor code. The author intends to fill a list with the selection value of the promotion enumerated, and each value shows the corresponding display name.

Solution

In order to solve this problem, we use an extension method to make the province in the province and search for specific attributes applied to its members. The following is the code of the expansion method:

This method is used as a general method to retrieve any attributes applied to enumeration members. In our example, we want to retrieve the [Display] property to obtain its name attribute.

<code class="language-csharp">public static class Extensions
{
    public static TAttribute GetAttribute<TAttribute>(this Enum enumValue)
        where TAttribute : Attribute
    {
        return enumValue.GetType()
            .GetMember(enumValue.ToString())
            .First()
            .GetCustomAttribute<TAttribute>();
    }
}</code>
Copy after login
Implementation in Razor view

Using the expansion method, we can now modify the Razor code as follows:

This code retrieves the [Display] property of the current promotion, and access its name property to display the corresponding display name in the list item. Note that the modified code uses

for type conversion, which is more secure and reliable to obtain attributes.

<code class="language-csharp">@foreach (int aPromotion in Enum.GetValues(typeof(UserPromotion)))
{
    var currentPromotion = (int)Model.JobSeeker.Promotion;
    if ((currentPromotion & aPromotion) == aPromotion)
    {
       var displayName = ((UserPromotion)aPromotion).GetAttribute<DisplayAttribute>().Name;
       <li>@displayName</li>
    }
}</code>
Copy after login
Example usage

((UserPromotion)aPromotion)

Related example demonstration, please refer to the following code fragment:

Output:

<code class="language-csharp">public class Foo
{
    public Season Season { get; set; }

    public void DisplayName()
    {
        var seasonDisplayName = Season.GetAttribute<DisplayAttribute>();
        Console.WriteLine("Which season is it?");
        Console.WriteLine(seasonDisplayName?.Name ?? "Unknown"); // 使用空合并运算符处理可能为null的情况
    }
}

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
}</code>
Copy after login
The improved code is more robust, handling potential air citation abnormalities, and more clearly showing how to correctly use the extension method in the Razor view to obtain the display name.

The above is the detailed content of How to Retrieve Enum Display Names in ASP.NET MVC Razor Views?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template