2014-05-14 68 views
0

我想根據用戶是否登錄,有條件地隱藏EnumDropDownListFor中的元素。EnumDropDownListFor有條件地隱藏項目

枚舉

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 
} 

我期待隱藏項目PriceAsc & PriceDesc
我試圖尋找到AutoGenerateFilterAutoGenerateField性能無濟於事。

查看

@Html.EnumDropDownListFor(x => x.sortType, new { id = "orderResults" }) 
+0

如何拆解,檢查user.identity.isauthenticated的EnumDropDownListFor並實現自定義的幫手? – heymega

+0

這是我以前如何做到這一點,我希望有一些不錯的MVC 5.1方法做到這一點:) –

+0

你可以在這裏找到解決方案http://stackoverflow.com/questions/27133014/exclude-remove-value - 從-MVC-5-1-enumdropdownlistfor –

回答

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