MVC 5.1 Razor DisplayFor not working with Enum DisplayName [英] MVC 5.1 Razor DisplayFor not working with Enum DisplayName

查看:13
本文介绍了MVC 5.1 Razor DisplayFor not working with Enum DisplayName的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下包含枚举的实体(域)对象和模型.显示名称正确显示并且适用于 EnumDropdownList 但由于某种原因不适用于 DisplayFor 助手,显示的只是实际的枚举名称.

I have the following entity (domain) object and model that contain an enum. The display name appears correctly and works for a EnumDropdownList but for some reason not for the DisplayFor helper, all that is shown is the actual enum name.

不确定我缺少什么,asp.net MVC 5.1 为此添加了显示名称支持,因此我不需要创建自己的辅助方法.请参阅:https://aspnet.codeplex.com/SourceControl/latest#示例/MVC/EnumSample/EnumSample/Models/Enums.cs

Not sure what I am missing, asp.net MVC 5.1 added display name support for this so I shouldn't need to create my own helper methods. See: https://aspnet.codeplex.com/SourceControl/latest#Samples/MVC/EnumSample/EnumSample/Models/Enums.cs

public class Addon
{
    public int Id { get; set; }
    public AddonType AddonType { get; set; }
    public string Name { get; set; }
    public decimal Price { get; set; }
    public bool IsActive { get; set; }
}

public enum AddonType : byte
{
    [Display(Name = "Cake Theme")]
    CakeTheme,
    [Display(Name = "Cake Flavour")]
    CakeFlavour,
    [Display(Name = "Cupcake Icing")]
    CupcakeIcing,
    [Display(Name = "Party Addon")]
    AddOn
}

模型

public class AddonModel
{
    public int Id { get; set; }
    public AddonType AddonType { get; set; }
    public string Name { get; set; }
    public decimal Price { get; set; }
    public int Quantity { get; set; }
    public bool IsActive { get; set; }
}

查看

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table class="table">
    <tr>
        <th>Type</th>
        <th>Name</th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(model => item.AddonType)
        </td>
        <td>
            @Html.DisplayFor(model => item.Name)
        </td>
        <td>
            @Html.DisplayFor(model => item.Price)
        </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>
}

</table>

推荐答案

新建文件夹 Views/Shared/DisplayTemplates
将名为 Enum 的空局部视图添加到文件夹
将枚举视图代码替换为:

Create new folder Views/Shared/DisplayTemplates
Add empty Partial View named Enum, to the folder
Replace Enum View code with:

@model Enum

@if (EnumHelper.IsValidForEnumHelper(ViewData.ModelMetadata))
{
    // Display Enum using same names (from [Display] attributes) as in editors
    string displayName = null;
    foreach (SelectListItem item in EnumHelper.GetSelectList(ViewData.ModelMetadata, (Enum)Model))
    {
        if (item.Selected)
        {
            displayName = item.Text ?? item.Value;
        }
    }

    // Handle the unexpected case that nothing is selected
    if (String.IsNullOrEmpty(displayName))
    {
        if (Model == null)
        {
            displayName = String.Empty;
        }
        else
        {
            displayName = Model.ToString();
        }
    }

    @Html.DisplayTextFor(model => displayName)
}
else
{
    // This Enum type is not supported.  Fall back to the text.
    @Html.DisplayTextFor(model => model)
}

这是 Shahriar Hossain 详细文章的链接

这篇关于MVC 5.1 Razor DisplayFor not working with Enum DisplayName的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆