除了第一项

时间:2018-06-11 15:09:23

标签: c# asp.net-mvc

这是枚举

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 个答案:

答案 0 :(得分:4)

您可以简单地使用if语句排除None选项,而不是排除“first”选项:

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 >
    }
}