【问题标题】:Add/Update IEnumerable List添加/更新 IEnumerable 列表
【发布时间】:2017-04-18 05:47:00
【问题描述】:

我只是想问一下如何传递/更新将用于在下拉列表中显示的 IEnumerable 列表,因为我缺少一组行来添加它并更新 enumlist 以便我只能获取文本和值为ArrivedCompleted。这是我的模型中的可枚举列表

    public enum DeliveryPermitStatus
    {

        Arrived = 1,
        Approved = 2,
        Cancelled = 3,
        Completed = 4,
        Submitted = 5
    }

我的控制器中的代码,以便我可以添加过滤后的enumlist,我缺少代码来查看更新后的enumlist,该代码将用于在下拉列表中显示

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

                if (User.IsInRole(StaticRoleNames.Admin)) //your condition here
                {
                    foreach(var item in enumlist)
                    {
                        if(item.Text == "Arrived" || item.Text == "Completed")
                        {
                           //Missing Code Here

                        }
                    }

                }

                ViewBag.enumlist = enumlist;

【问题讨论】:

    标签: c# linq enums ienumerable


    【解决方案1】:

    我认为您要/想要做的是将ViewBag.enumlist 编辑为根据用户角色减少的一组值?如果是这样,那么您可以在 if 语句中过滤和分配列表并放弃 foreach 循环:

    var enumlist = Enum.GetValues(typeof(DeliveryPermitStatus)).Cast<DeliveryPermitStatus>().Select(v => new SelectListItem
                {
                    Text = v.ToString(),
                    Value = ((int)v).ToString()
                });
    
                if (User.IsInRole(StaticRoleNames.Admin)) //your condition here
                {
                    ViewBag.enumlist = enumlist.Where(t =>  t.Text == "Arrived" || t.Text == "Completed");
                }
    

    或者,如果您需要保留 foreach 循环,那么您需要创建一个单独的列表来跟踪枚举列表的“可用值”:

    var enumlist = Enum.GetValues(typeof(DeliveryPermitStatus)).Cast<DeliveryPermitStatus>().Select(v => new SelectListItem
                {
                    Text = v.ToString(),
                    Value = ((int)v).ToString()
                });
    
                List<SelectListItem> availableOptions = new List<SelectListItem>();
    
                if (User.IsInRole(StaticRoleNames.Admin)) //your condition here
                {
                    foreach(var item in enumlist)
                    {
                        if(item.Text == "Arrived" || item.Text == "Completed")
                        {
                           availableOptions.Add(item);
                        }
                    }
    
                }
    
                ViewBag.enumlist = availableOptions;
    

    【讨论】:

    • 看起来似是而非的选项+1,否则问题本身的要求不是很清楚
    • 这绝对是我要找的!谢谢楼主!
    【解决方案2】:

    您不能将元素添加到 IEnumerable,但您可以轻松地从中获取 List。 只需在分配给您的 var 枚举列表之前调用 ToList()。

    现在你可以给它添加元素了(注意,枚举的时候不要添加元素)

    【讨论】:

      猜你喜欢
      • 2020-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-06
      • 1970-01-01
      • 2016-11-04
      • 1970-01-01
      • 2021-05-01
      相关资源
      最近更新 更多