Filter in DropDown using C # MVC 4

6

I have a dropdown in my view that I fill in as follows:

 ViewBag.NfeStatus = EnumHelper
      .ListAll<NfeStatus>()
      .ToSelectList(x => x, x => x.Description());

My NfeStatus model is an enum:

public enum NfeStatus
{
    [Description("-")]
    NotIssued = 0,
    [Description("Pendente")]
    Pending,
    [Description("Erro")]
    Error,
    [Description("Ok")]
    Ok
}

However, I would like to not display the "OK" option.

How do I accomplish this?

    
asked by Felipe Negro 17.01.2017 в 12:33

1 answer

9

Add a filter using .Where() .

Something like .Where(x => x != NfeStatus.Ok) .

Or .Where(x => x.Description() != "OK") if you prefer to search for the description, although I think there is no reason for it.

Example

ViewBag.NfeStatus = EnumHelper.ListAll<NfeStatus>()
                              .Where(x => x != NfeStatus.Ok)
                              .ToSelectList(x => x, x => x.Description());

Or, using the description:

ViewBag.NfeStatus = EnumHelper.ListAll<NfeStatus>()
                              .Where(x => x.Description() != "OK")
                              .ToSelectList(x => x, x => x.Description());
    
17.01.2017 / 12:34