Sundeep
Sundeep

Reputation: 2135

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

Upvotes: 3

Views: 1039

Answers (1)

Ehsan Sajjad
Ehsan Sajjad

Reputation: 62488

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 :

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

Hope it helps!

Upvotes: 5

Related Questions