【问题标题】:Display enum descriptions inside razor views在 razor 视图中显示枚举描述
【发布时间】:2016-03-08 23:54:50
【问题描述】:

我有一个具有以下枚举属性的模型:

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

}

在我的 razor 视图中,我想显示每个项目的枚举描述,例如جديد 而不是 New。我尝试按照this answer 中的说明进行操作,但我不知道在哪里添加扩展方法以及如何在我的剃刀视图文件中调用扩展方法。如果有人可以完成我的代码,我将不胜感激:

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

【问题讨论】:

  • 您可以将扩展方法放在任何地方(在您当前的程序集中,或另一个程序集或单独的 dll 中)。您只需将其用作&lt;td&gt;@item.Status.DisplayName()&lt;/td&gt;(并包含指向您程序集的必要using 语句。
  • @StephenMuecke 我在公共静态类Utils 中添加了DisplayName 方法,这使得在添加using ProjectManager.App_Start; 后可以在我的项目中以Utils.DisplayName 访问该方法,但是在@item.Status.DisplayName() 中没有解决它。我现在该怎么办?
  • 你遇到了什么错误?
  • @StephenMuecke 没有DisplayName 的重载方法,然后我注意到我在视图文件的开头错过了using ProjectManager.App_Start;,然后添加了它。看来我和这个 MVC 的东西有很大关系!您可以发表您的评论作为答案吗:)?
  • 我强烈建议您改用system.componentmodel.dataannotations.displayattribute,因为;它已经像 DescriptionAttribute 一样内置在 .Net 中,因为它已经拥有许多您将来很可能需要的属性。

标签: c# asp.net-mvc razor enums data-annotations


【解决方案1】:

您可以将扩展方法放在任何地方。例如在您当前的项目中,添加一个文件夹(比如)Extensions,然后添加一个静态类

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();
        }
    }
}

虽然我会考虑创建一个单独的项目并在您当前的项目中添加对它的引用,以便您可以在多个项目中使用它(以及其他有用的扩展方法)。

然后在视图中包含@using yourProject.Extensions; 语句并将其用作

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

还要注意,要避免视图中出现 using 语句,您可以将程序集添加到您的 web.config.cs 文件中

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-26
    • 1970-01-01
    • 2015-12-30
    • 2013-11-05
    • 2019-04-16
    • 2015-10-09
    • 2018-12-18
    相关资源
    最近更新 更多