2017-03-10 100 views
2

局部枚舉列表比方說,我有一個枚舉這樣的:顯示在ASP.NET核心下拉列表

public enum ContactPhoneType 
{ 
    [Display(Name = "")] 
    None = 0, 

    [Display(Name = "Home Phone")] 
    HomePhone = 1, 

    [Display(Name = "Cell/Mobile Phone")] 
    CellMobile = 2, 

    [Display(Name = "Work Phone")] 
    Work = 3, 

    [Display(Name = "Family Member")] 
    FamilyMember = 4, 

    [Display(Name = "Fax Number")] 
    Fax = 5, 

    [Display(Name = "Other")] 
    Other = 6, 
} 

我想從列表中只顯示第6。我怎樣才能隱藏最後一個?

對於示出的所有項目中,我使用下面的代碼:

<div class="form-group"> 
    <label class="col-sm-4 control-label" asp-for="PhoneNumberType"></label> 
     <div class="col-sm-6"> 
     <select asp-for="PhoneNumberType" asp-items="Html.GetEnumSelectList<ContactPhoneType>()" class="form-control"></select> 
     </div> 
</div> 

回答

3

如果該方法返回,從IEnumerable<T>繼承集合,則可以使用Take()方法來選擇以下的其元件的第一N個方式:

asp-items="Html.GetEnumSelectList<ContactPhoneType>().Take(6)" 

希望它有幫助!