Asp.net mvc “我的枚举”类型的项未转换

Asp.net mvc “我的枚举”类型的项未转换,asp.net-mvc,Asp.net Mvc,我有以下视图模型: public class BudgetTypeSiteRowListViewModel { public virtual int BudgetTypeSiteID { get; set; } public virtual string SiteName { get; set; } public virtual BudgetTypeEnumViewModel SiteType { get; set; } } 使用以下枚举: public

我有以下视图模型:

public class BudgetTypeSiteRowListViewModel
{
    public virtual int BudgetTypeSiteID { get; set; }
    public virtual string SiteName { get; set; }
    public virtual BudgetTypeEnumViewModel SiteType { get; set; }        
}
使用以下枚举:

public enum BudgetTypeEnumViewModel
{
    [Display(Name = "BudgetTypeDaily", ResourceType = typeof (UserResource))] Daily = 1,
    [Display(Name = "BudgetTypeRevision", ResourceType = typeof (UserResource))] Revision = 2
}
以及以下列出我的项目的视图:

@model IEnumerable<BudgetTypeSiteRowListViewModel>

<table>
    @foreach (var item in Model)
    {
        <tr>
            <td>@Html.DisplayFor(m => item.SiteName)</td>
            <td>@Html.DisplayFor(m => item.SiteType)</td>
        </tr>
    }
</table>
问题是我列出的项目不符合正确的区域性。我有“每日”或“修订版”,我应该有“Journalier”或“Dagelijkse”或“Révision”或“Revisie”

如何使我的站点类型处于枚举提供的正确区域性中


谢谢。

您必须编写一个使用反射获取属性枚举类型的扩展方法

public static string DisplayAttribute<TEnum>(this TEnum enumValue) where TEnum : struct
{
  //You can't use a type constraints on the special class Enum. So I use this workaround
  if (!typeof(TEnum).IsEnum)
    throw new ArgumentException("TEnum must be of type System.Enum");

  Type type = typeof(TEnum);
  MemberInfo[] memberInfo = type.GetMember(enumValue.ToString());
  if (memberInfo != null && memberInfo.Length > 0)
  {
    object[] attrs = memberInfo[0].GetCustomAttributes(typeof(DisplayAttribute), false);
    if (attrs != null && attrs.Length > 0)
      return ((DisplayAttribute)attrs[0]).GetName();
  }
  return enumValue.ToString();
}
我希望有帮助

@Html.DisplayFor(m => item.SiteType.DisplayAttribute())