【问题标题】:Get Enum Display values from table in asp.net MVC view [duplicate]从asp.net MVC视图中的表中获取枚举显示值[重复]
【发布时间】:2016-06-20 10:35:28
【问题描述】:

我有枚举值和使用 DataAnnotations 的 Display 属性,在 Display 属性的下拉列表值中正确显示,但是当从数据库中检索数据时,它显示的值不是使用 DataAnnotations 分配的显示属性文本。我如何在我的视图中获取显示值。 我的枚举

public enum CareerLevel
        {
            [Display(Name = "Entry Level")]
            Level1,
            [Display(Name = "Experienced Professional")]
            Level2,
            [Display(Name = "Department Head")]
            Level3      
        }

这是我想要显示“入门级”等值的视图

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

它显示 Level1 而不是 Entry Level 。我应该在我的视图或枚举中进行哪些更改??

【问题讨论】:

  • this answer中的扩展可以用来解决你的问题吗?
  • 我相信最简洁的实现是创建一个新的显示模板格式链接到重复标志中,这样您的视图比在视图中调用扩展方法要干净得多。
  • 实施这个解决了我的问题,而视图没有任何变化codeproject.com/Articles/776908/Dealing-with-Enum-in-MVC

标签: c# asp.net-mvc enums


【解决方案1】:

您可以创建一个扩展方法来执行此操作;

public static class Extensions
{

public static TAttribute GetAttribute<TAttribute>(this Enum enumValue) 
        where TAttribute : Attribute
{
    return enumValue.GetType()
                    .GetMember(enumValue.ToString())
                    .First()
                    .GetCustomAttribute<TAttribute>();
}
}

你可以这样使用它;

var level = CareerLevel.Level1

var name = level.GetAttribute<DisplayAttribute>().Name;

在您自己的代码中,您可以像这样使用它;

@Html.DisplayFor(modelItem => item.CareerLevel.GetAttribute<DisplayAttribute>().Name)

无论您想使用扩展方法,都必须引用您的 Extensions 类命名空间。

【讨论】:

    【解决方案2】:

    在我的例子中,使用了以下扩展方法:

    public static class MVCExtentions
    {
    
    public static string GetInputName<TModel, TProperty>(Expression<Func<TModel, TProperty>> expression)
    {
        if (expression.Body.NodeType == ExpressionType.Call)
        {
            MethodCallExpression methodCallExpression = (MethodCallExpression)expression.Body;
            string name = GetInputName(methodCallExpression);
            return name.Substring(expression.Parameters[0].Name.Length + 1);
    
        }
        return expression.Body.ToString().Substring(expression.Parameters[0].Name.Length + 1);
    }
    
    private static string GetInputName(MethodCallExpression expression)
    {
        MethodCallExpression methodCallExpression = expression.Object as MethodCallExpression;
        if (methodCallExpression != null)
        {
            return GetInputName(methodCallExpression);
        }
        return expression.Object.ToString();
    }
    
    public static MvcHtmlString EnumDropDownListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression) where TModel : class
    {
        string inputName = GetInputName(expression);
        var value = htmlHelper.ViewData.Model == null ? default(TProperty): expression.Compile()(htmlHelper.ViewData.Model);
        return htmlHelper.DropDownList(inputName, ToSelectList(typeof(TProperty), value.ToString()));
    }
    }
    

    在视图中,我使用以下符号:

     @Html.EnumDropDownListFor(model => model.CareerLevel, new { @class = "form-control" })
    

    我的 ViewModel 如下所示:

    [EnumDataType(typeof(CareerLevel))]
    [Display(Name = "Level")]
    [DefaultValue(CareerLevel.Level1)]
    public CareerLevel CareerLevelType { get; set; }
    

    希望能给你一点帮助...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-28
      • 1970-01-01
      • 1970-01-01
      • 2013-09-24
      • 1970-01-01
      • 2015-08-31
      • 2012-08-11
      相关资源
      最近更新 更多