Dropdown filter using C# MVC 4

Asked

Viewed 289 times

6

I have a dropdown in my view that I fill in the following way:

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

My model Nfestatus is an Enum:

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

But I would like to not display the "OK".

How I do this friltro?

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());

Browser other questions tagged

You are not signed in. Login or sign up in order to post.