具有字符串名称的枚举数组

时间:2019-11-11 10:17:18

标签: c# razor enums asp.net-core-mvc

您好,我在复选框列表中有一个Enum数组(用于多个选择项)。 我想显示复选框标签的字符串基本名称。  我曾经无法在我的视图中显示字符串开关 代码:

    public enum ServiceArea
    {
        // [Description("Not Completed")]
        AllOver,
        North,
        south,
        Center,
        Sharon,
    }
    public class SupplayerViewModel
    {
      ....

        public ServiceArea [] serviceArea { get; set; }
        public ServiceArea  servArae { get; set; }
        public string EnumfriendlyName => servArae.GetUserEnumName();
    }
    public static class EnumfriendlyName
    {
        public static string GetUserEnumName(this ServiceArea serviceArea)
        {
            switch (serviceArea)
            {
                case ServiceArea.AllOver: return "All Over ";
                case ServiceArea.Center:return "cc ccc";
                case ServiceArea.North:return "n n n";
                case ServiceArea.Sharon:return "sh sh";
                case ServiceArea.south:return "ss s";
            }

            return "Failure!!";
        }
    }
    public class Supplayer
    {
     ...
        public  ServiceArea [] serviceArea { get; set; }

       ...
    }

我的剃刀视图:

 @foreach (var item in Enum.GetValues(typeof(ServiceArea)))
                {
                    <label>
                        <i class="fas fa-street-view"></i> @item
                        <input type="checkbox" value="@item" name="ServiceArea" />
                    </label>
                }

1 个答案:

答案 0 :(得分:0)

很好地得到它 @ EnumfriendlyName.GetUserEnumName(((ServiceArea)@item)