VSB
VSB

Reputation: 10375

Display enum descriptions inside razor views

I have a model with enumerated properties as below:

namespace ProjectManager.Models
{
    public class Contract
    {
        .....
        public enum ContractStatus
        {
            [System.ComponentModel.Description("جديد")]
            New,
            [System.ComponentModel.Description("در انتظار پرداخت")]
            WaitForPayment,
            [System.ComponentModel.Description("پرداخت شده")]
            Paid,
            [System.ComponentModel.Description("خاتمه يافته")]
            Finished
        };

        public ContractStatus Status { get; set; }
        .....
    }

}

Inside my razor views, I want to display enum descriptions for each item, e.g. جديد instead of New. I tried to follow instructions in this answer, but I don't know where to add extension method and how to call extension method inside my razor view file. I would be thankful if someone can complete my code:

@model IEnumerable<ProjectManager.Models.Contract>
....
<table class="table">
    <tr>
        .....
        <th>@Html.DisplayNameFor(model => model.Status)</th>
        .....
    </tr>

@foreach (var item in Model) {
    <tr>
        ......
        <td>
            @Html.DisplayFor(modelItem => item.Status)  //<---what should i write here?
        </td>
        ....
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.Id }) |
            @Html.ActionLink("Details", "Details", new { id=item.Id }) |
            @Html.ActionLink("Delete", "Delete", new { id = item.Id })|
        </td>
    </tr>
}

Upvotes: 2

Views: 4603

Answers (2)

Lenny Woods
Lenny Woods

Reputation: 361

use Display attribute with your enum class

public enum ContractStatus
{
    [Display(Name = "جديد")]
    New,
    [Display(Name = "در انتظار پرداخت")]
    WaitForPayment,
    [Display(Name = "پرداخت شده")]
    Paid,
    [Display(Name = "خاتمه يافته")]
    Finished
};

then in your view: (nothing changes)

<th>@Html.DisplayNameFor(model => model.Status)</th>

Upvotes: 0

user3559349
user3559349

Reputation:

You can put the extension method anywhere. For example in your current project, add a folder (say) Extensions and then add a static class

namespace yourProject.Extensions
{
    public static class EnumExtensions
    {
        public static string DisplayName(this Enum value)
        {
            // the following is my variation on the extension method you linked to
            if (value == null)
            {
                return null;
            }
            FieldInfo field = value.GetType().GetField(value.ToString());
            DescriptionAttribute[] attributes = (DescriptionAttribute[])field
                .GetCustomAttributes(typeof(DescriptionAttribute), false);
            if (attributes.Length > 0)
            {
                return attributes[0].Description;
            }
            return value.ToString();
        }
    }
}

although I would consider creating a separate project and add a reference to it in your current project so that you can use it (and other useful extension methods) across multiple projects.

Then include a @using yourProject.Extensions; statement in the view and use it as

<td>@item.Status.DisplayName()</td>

Note also that to avoid the using statements in the view, you can add the assembly to your web.config.cs file

<system.web>
    ....
    <pages>
        <namespaces>
            <add namespace="System.Web.Helpers" />
            <add namespace="yourProject.Extensions" />

Upvotes: 9

Related Questions