【问题标题】:EnumDropDownListFor Conditionally hide itemsEnumDropDownListFor 有条件地隐藏项目
【发布时间】:2014-05-14 09:15:13
【问题描述】:

我想根据用户是否登录有条件地隐藏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" })

【问题讨论】:

  • 如何取消 EnumDropDownListFor 并实现您自己的自定义帮助程序来检查 user.identity.isauthenticated?
  • 这就是我以前会这样做的方式,我希望有一些不错的 MVC 5.1 方式来做到这一点:)
  • 你可以在这里找到解决方案stackoverflow.com/questions/27133014/…

标签: c# asp.net asp.net-mvc asp.net-mvc-5


【解决方案1】:
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() });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-08
    • 2014-06-14
    • 1970-01-01
    • 2020-03-02
    • 1970-01-01
    • 2020-09-24
    相关资源
    最近更新 更多