Jan-Maarten Verweij
Jan-Maarten Verweij

Reputation: 41

Filter EnumDropDownListFor ASP.NET MVC5

In my ASP.NET MVC 5 app I have an Enum:

public enum cars
{
  Audi = 1,
  BMW = 2,
  Ferrari = 3
 }

In my view I use an EnumDropDownListFor to select one of those values.

    Html.EnumDropDownListFor(m=>m.car)

Is there a way to filter this list so it only shows eg. Audi + BMW?

Upvotes: 4

Views: 1121

Answers (1)

Haroon
Haroon

Reputation: 1110

An old question, if you have the flexibility to change the enums to be power of 2, e.g. 1,2,4,8... you can use bitwise operation on the enum.

public class CarModel
    {
        public Cars MyCar
        {
            get { return Cars.Audi | Cars.VW | Cars.Cadalic;}
            set { ; }
        }

        [Flags]
        public enum Cars
        {
            Audi=1,
            Bmw=2,
            VW=4,
            Cadalic=8
        }
    }

Upvotes: 2

Related Questions