【问题标题】:How to Filter Enum and Use it in Dropdown如何过滤枚举并在下拉列表中使用它
【发布时间】:2017-04-17 08:36:04
【问题描述】:

我是MVC 5 的新手,我的目标是过滤我的enum 中的列表,我将在我的下拉列表中显示

public enum DayofWeekType
{
      Monday=1,
      Tuesday= 2,
      Wednesday=3,
      Thursday=4,
      Friday= 5,
      Saturday=6,
      Sunday= 7
}

当登录的用户不是管理员时,我只想在下拉列表中显示星期五、星期六和星期日,我无法找到过滤Model 中的enum 字段的解决方案,尝试在模型中添加条件但总是总结为错误。尝试搜索LINQjQuery 解决方案。

【问题讨论】:

  • 只是枚举定义。你的过滤代码在哪里?你的代码有什么问题?你有错误吗?值没有被过滤?目前看来您希望我们从头到尾解决您的任务
  • 你的模型是什么样子的?
  • @SergeyBerezovskiy 不,先生,我只是想知道这里是否有解决方法
  • 你能显示你的视图代码吗?

标签: c# jquery linq enums


【解决方案1】:

你可以这样做

   var enumlist =  Enum.GetValues(typeof(DayofWeekType)).Cast<DayofWeekType>().Select(v => new SelectListItem
    {
        Text = v.ToString(),
        Value = ((int)v).ToString()
    });

    if (IsUser) //your condition here
    {
      enumlist=  enumlist.Skip(4);

    }

    ViewBag.enumlist = enumlist;

在你看来

@Html.DropDownListFor(x=>x.Id,(IEnumerable<SelectListItem>) ViewBag.enumlist)

.Skip 将首先跳过4 值并以5th 值开头,即Friday

【讨论】:

    【解决方案2】:
    var weekValues = System.Enum.GetValues(typeof(DayofWeekType));
    var weekNames = System.Enum.GetNames(typeof(DayofWeekType));
    
    for (int i = 0; i <= weekNames.Length - 1 ; i++) {
        ListItem item = new ListItem(weekNames[i], weekValues[i]);
        ddlList.Items.Add(item);
    }
    

    【讨论】:

      【解决方案3】:

      您必须按照以下步骤在控制器级别对其进行过滤以实现

                  int[] eliminateDays = null;
                  // wrap enum into collection
                  var enumDaysCollection = (from dayName in Enum.GetNames(typeof(DayofWeekType))
                                        select new
                                        {
                                            Text = dayName.ToString(),
                                            Value = (int)Enum.Parse(typeof(DayofWeekType), dayName)
                                        }).ToList();
                  // array contain days (enum values) which will be ignored as per user specific
                  // lets say you want to ignore monday, tuesday, wednesday, thursday 
                  if (User.IsInRole("SomeUserRole"))
                  {
                      eliminateDays = new int[] { 1, 2, 3, 4 };
                  }
                  else if (User.IsInRole("AnotherUserRole"))
                  {
                      eliminateDays = new int[] { 1, 3 };
                  }
                  else
                  {
                      //For Admin will make empty so that it will show all days
                      eliminateDays = new int[] { };
                  }
                  // filter collection
                  var dropDownItems = (from day in enumDaysCollection
                                       let days = eliminateDays
                                       where !days.Contains(day.Value)
                                       select new SelectListItem
                                       {
                                           Text = day.Text,
                                           Value = Convert.ToString(day.Value)
                                       }).ToList();
                  //send dropdownlist values to view
                  ViewBag.DropDownItems = dropDownItems; 
      

      最后将 SelectListItem 集合分配给下拉列表

      @Html.DropDownList("DaysName", (List<SelectListItem>)ViewBag.DropDownItems, "Select Days..", new { @class = "dropdown" })
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-10-15
        • 2011-04-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多