代码之家  ›  专栏  ›  技术社区  ›  user3250818

如何从第一个项以外的项foreach枚举

  •  0
  • user3250818  · 技术社区  · 6 年前

    public enum SportTypeEnum
    {
        [EnumDescription("not defined")]
        Null = 0,
    
        [EnumDescription("football")]
        FootBall = 100,
    
        [EnumDescription("volyball")]
        VollyBall = 110,
    
        [EnumDescription("basketball")]
        BasketBall = 120,
    
        [EnumDescription("Swimming")]
        wrestling = 140,
    }
    

    我可以循环遍历下面的所有元素

    var sportTypeValueList = Enum.GetValues(typeof(SportTypeEnum));
    @foreach (SportTypeEnum sportTypeEnum in sportTypeValueList)
    {                   
        <option value="@(sportTypeEnum.ToString())" @(dataUi != null && dataUi.SportType == sportTypeEnum ? "selected=\"selected\"" : "")>
            @EnumUtilities.GetEnumDescription(sportTypeEnum)
        </option>
    }
    

    1 回复  |  直到 6 年前
        1
  •  4
  •   mm8    6 年前

    None 选项而不是排除“第一”选项:

    var sportTypeValueList = Enum.GetValues(typeof(SportTypeEnum));
    @foreach(SportTypeEnum sportTypeEnum in sportTypeValueList)
    {
        @if(sportTypeEnum != SportTypeEnum.Null)
        {
            <option value = "@(sportTypeEnum.ToString())" @(dataUi != null && dataUi.SportType == sportTypeEnum ? "selected=\"selected\"" : "") >
               @EnumUtilities.GetEnumDescription(sportTypeEnum)
            </option >
        }
    }