简体   繁体   English

在ASP.NET Core下拉列表中显示部分枚举列表

[英]Show partial Enum list in ASP.NET Core Dropdown list

Let's say I have an enumeration like this: 假设我有一个这样的枚举:

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

I want to display only the first 6 from the list. 我想只显示列表中的前6个。 How can I hide the last one? 我怎么能隐藏最后一个?

For showing all the items, I have used the below code: 为了显示所有项目,我使用了以下代码:

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

If the method returns a collection that inherits from IEnumerable<T> , you can use Take() method to select first N number of elements of it following way : 如果该方法返回一个继承自IEnumerable<T>的集合,则可以使用Take()方法按以下方式选择其前N个元素:

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

Hope it helps! 希望能帮助到你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM