当前位置: 代码迷 >> 综合 >> [Flags]标识的Enum不能使用Html.GetEnumSelectList方法
  详细解决方案

[Flags]标识的Enum不能使用Html.GetEnumSelectList方法

热度:53   发布时间:2024-01-12 09:39:07.0

在使用Asp.Net Core MVC写程序时,对用户类型做了如下定义:

namespace ManufacturingExecutionSystemCore.Enums
{public enum UserType{[Description("无身份人员")][Display(Name = "无身份人员")]UnidentifiedPerson=0x00,[Description("生产人员")][Display(Name = "生产人员")]ProductionWorker = 0x01,[Description("生产主管")][Display(Name = "生产主管")]ProductionHead =0x02,[Description("生产经理")][Display(Name = "生产经理")]ProductionManager = 0x04,[Description("仓库人员")][Display(Name = "仓库人员")]WarehouseWorker = 0x08,[Description("仓库主管")][Display(Name = "仓库主管")]WarehouseHead =0x10,[Description("仓库经理")][Display(Name = "仓库经理")]WarehouseManager = 0x20,[Description("质检人员")][Display(Name = "质检人员")]QualityCheckWorker =0x40,[Description("质检主管")][Display(Name = "质检主管")]QualityCheckHead = 0x80,[Description("质检经理")][Display(Name = "质检经理")]QualityCheckManager =0x100,}
}

在写Razor时,使用

<select asp-for="UserType" class="form-control" asp-items="@Html.GetEnumSelectList<UserType>()"></select>

运行时发现报错:

An unhandled exception occurred while processing the request.
ArgumentException: The type 'ManufacturingExecutionSystemCore.Enums.UserType' is not supported. Type must be an enum that does not have an associated FlagsAttribute. (Parameter 'TEnum')
Microsoft.AspNetCore.Mvc.ViewFeatures.HtmlHelper.GetEnumSelectList<TEnum>()

为记录Html.GetEnumSelectList<UserType>()不支持FlagsAttribute标识的Enum,特作此记录。

如果各位有其他方法可以共同讨论。

  相关解决方案