vlambda博客
学习文章列表

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


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

在使用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,特作此记录。

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


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

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

— END —

        回复:新书   获取《ASP.NET Core 3框架揭秘》

        回复:进阶   获取 《CLR via C#(第4版)》购书链接

        回复:本质论   获取 《C# 7.0本质论》购书链接

        回复:WPF   获取 WPF 电子书


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