如何找到相应的值的枚举名称,并在DisplayFor使用它? [英] How find the enum name for corresponding value and use it in DisplayFor?

查看:201
本文介绍了如何找到相应的值的枚举名称,并在DisplayFor使用它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要为内部DisplayFor的HtmlHelper相应值显示枚举的名称。我有以下枚举:

I need to display the name of the enum for corresponding value inside DisplayFor HtmlHelper. I have the following enum:

public enum CheckStatus
    {
        Yes = 1,
        No = 2,
        Maybe =3
    }

我的模型通常显示的值是这样的:

I'm displaying values for a model normally like this:

@Html.DisplayFor(modelItem => item.Name)

问题是,在一个点上我有这样的:

The problem is that at one point I have this:

@Html.DisplayFor(modelItem => item.Status)

这行只显示这是从枚举之前设置状态值(1,2或3)。相反的,我需要以某种方式该值显示名称。所以,如果状态code是2,我想显示否,而不是2号。

That line displays only status value which is set before from enum (1,2 or 3). Instead of that I need somehow to display name for that value. So, if status code is 2, I want to display 'No', not number 2.

我有类似的问题,得到枚举的名字时,我填充下拉列表,我设法解决这个问题是这样的:

I had the similar problem with getting enum names when I was populating dropdown list and I managed to solve it like this:

@Html.DropDownListFor(model => model.Item.Status,
                new SelectList(Enum.GetValues(typeof(Pro.Web.Models.Enums.CheckStatus))))

我是如何从枚举值只得到一个名字一点点丧失。

I am a little bit lost in how to get only that one name from the value of the enum.

感谢您的帮助。

推荐答案

这不是从你的问题有什么基本类型状态属性非常清晰。如果是的checkStatus ,那么 @ Html.DisplayFor(modelItem => item.Status)将显示正是你期望。如果在另一方面,它是一个整数,你可以写一个自定义的助手来显示正确的值:

It's not very clear from your question what's the underlying type of the Status property. If it is CheckStatus, then @Html.DisplayFor(modelItem => item.Status) will display exactly what you expect. If on the other hand it is an integer you could write a custom helper to display the proper value:

public static class HtmlExtensions
{
    public static IHtmlString DisplayEnumFor<TModel>(this HtmlHelper<TModel> html, Expression<Func<TModel, int>> ex, Type enumType)
    {
        var value = (int)ModelMetadata.FromLambdaExpression(ex, html.ViewData).Model;
        string enumValue = Enum.GetName(enumType, value);
        return new HtmlString(html.Encode(enumValue));
    }
}

,然后用它是这样的:

and then use it like this:

@Html.DisplayEnumFor(modelItem => item.Status, typeof(CheckStatus))

让我们假设你希望把这个帮助了一步,考虑到您的枚举类型的显示名称属性:

and let's suppose that you wanted to bring this helper a step further and take into account the DisplayName attribute on your enum type:

public enum CheckStatus
{
    [Display(Name = "oh yeah")]
    Yes = 1,
    [Display(Name = "no, no, no...")]
    No = 2,
    [Display(Name = "well, dunno")]
    Maybe = 3
}

下面是如何可以扩展我们的自定义帮助:

Here's how you could extend our custom helper:

public static class HtmlExtensions
{
    public static IHtmlString DisplayEnumFor<TModel>(this HtmlHelper<TModel> html, Expression<Func<TModel, int>> ex, Type enumType)
    {
        var value = (int)ModelMetadata.FromLambdaExpression(ex, html.ViewData).Model;
        string enumValue = Enum.GetName(enumType, value);
        var field = enumType.GetField(enumValue);
        if (field != null)
        {
            var displayAttribute = field
                .GetCustomAttributes(typeof(DisplayAttribute), false)
                .Cast<DisplayAttribute>()
                .FirstOrDefault();
            if (displayAttribute != null)
            {
                return new HtmlString(html.Encode(displayAttribute.Name));
            }
        }
        return new HtmlString(html.Encode(enumValue));
    }
}

这篇关于如何找到相应的值的枚举名称,并在DisplayFor使用它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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