2

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>
}

Community
  • 1
  • 1
VSB
  • 9,825
  • 16
  • 72
  • 145
  • You can put the extension method anywhere (in you current assembly, or another assembly or a separate dll). And you just use it as `@item.Status.DisplayName()` (and include the necessary `using` statement that points to you assembly. –  Mar 09 '16 at 00:00
  • @StephenMuecke I added DisplayName method inside a public static class `Utils` which makes method accessible as `Utils.DisplayName` inside my project after adding `using ProjectManager.App_Start;` however it is not resolved in `@item.Status.DisplayName()`. What should I do now? – VSB Mar 09 '16 at 00:08
  • What errors are you getting? –  Mar 09 '16 at 00:12
  • @StephenMuecke No overload method for `DisplayName` and then I notice that i missed `using ProjectManager.App_Start;` at begining of view file and after adding that it worked. It seems that I have much to do with this MVC thing! Can you post your comment as an answer please:)? – VSB Mar 09 '16 at 00:19
  • I'd highly recommend you use [`system.componentmodel.dataannotations.displayattribute`](https://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.displayattribute(v=vs.110).aspx) instead because; it is already built into .Net like the `DescriptionAttribute` and because it already has many properties you will most likely need in the future. – Erik Philips Mar 09 '16 at 01:07

1 Answers1

8

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" />