0

I want to conditionally hide the elements in the EnumDropDownListFor based on if the user is logged in or not.

Enum

public enum SortType
{
    [Display(ResourceType = typeof(NavigationItems), Name = "BestMatch")]
    Best_Match = 0,
    [Display(ResourceType = typeof(NavigationItems), Name = "Alphabetical")]
    Alphabetical,
    [Display(ResourceType = typeof(NavigationItems), Name = "PriceAsc")]
    PriceAsc,
    [Display(ResourceType = typeof(NavigationItems), Name = "PriceDesc")]
    PriceDesc
}

The items I am looking to hide are PriceAsc & PriceDesc
I have tried looking into the AutoGenerateFilter and AutoGenerateField properties to no avail.

View

@Html.EnumDropDownListFor(x => x.sortType, new { id = "orderResults" })
Ashley Medway
  • 7,151
  • 7
  • 49
  • 71
  • How about scrapping the EnumDropDownListFor and implement your own custom helper that checks user.identity.isauthenticated? – heymega May 14 '14 at 09:24
  • This is how I would have done this previously, I was hoping there was some nice MVC 5.1 way of doing this :) – Ashley Medway May 14 '14 at 09:25
  • You can find the solution here http://stackoverflow.com/questions/27133014/exclude-remove-value-from-mvc-5-1-enumdropdownlistfor – Aladdin Bahian Mar 28 '16 at 07:15

1 Answers1

-2
var sortTypesToExclude = new[] { SortType.PriceAsc, SortType.PriceDesc }.Cast<int>();
var sortTypes = Enum.GetValues(typeof(SortType)).Cast<int>().Where(i => userLoggedIn || !sortTypesToExclude.Contains(i));
var sortTypesAsSelectList = sortTypes.Select(i => new SelectListItem() { Value = i.ToString(), Text = ((SortType)i).ToString() });