简体   繁体   English

从C#中的SelectList中删除项目

[英]Remove item from SelectList in C#

I have a method in which return me selectList. 我有一个方法,其中返回我selectList。 The certain items of selectList are to be programatically removed on the basis of Key. selectList的某些项目将基于Key以编程方式删除。

 public SelectList GetRoutingActionList()
    {
        Dictionary<string, string> routingActions = new Dictionary<string, string> { 
                                                        { "Approved", "Approve" },
                                                        { "Return To", "Return To .." }, 
                                                        { "Return To Sender", "Return To Sender" }, 
                                                        { "Disapprove", "Disapprove" },
                                                        { "Set On Hold", "Set On Hold" } 
                                                    };

        SelectList routingActionList = new SelectList(routingActions, "Key", "Value");
        return routingActionList;
    }

For eaxmple , I have to remove the item with Key "Disapprove". 对于eaxmple,我必须使用Key“Disapprove”删除该项目。 Could any one kindly help me to solve this. 任何人都可以帮助我解决这个问题。

You have to use Where() to filter them and then pass returned object of List<SelectListItem> to SelectList constructor to get the new SelectList which will not have item with Value "Disapprove" this way: 你必须使用Where()来过滤它们,然后将List<SelectListItem>返回对象传递给SelectList构造函数,以获得新的SelectList,它不具有值为“Disapprove”的项目:

public class SomeController
{

   public ActionResult Index()
   {

       SelectList list = GetRoutingActionList();

       list  = new SelectList(list 
                              .Where(x => x.Value != "Disapprove")
                              .ToList(),
                              "Value",
                              "Text");

        return View();
   }

}
public SelectList GetRoutingActionList()
    {
        Dictionary<string, string> routingActions = new Dictionary<string, string> { 
                                                        { "Approved", "Approve" },
                                                        { "Return To", "Return To .." }, 
                                                        { "Return To Sender", "Return To Sender" }, 
                                                        { "Disapprove", "Disapprove" },
                                                        { "Set On Hold", "Set On Hold" } 
                                                    };

        SelectList routingActionList = new SelectList(routingActions, "Key", "Value");
        return routingActionList.Where(x => x.Key != "Disapprove")
                                   .ToList();
}

Simply remove it from dictionary. 只需将其从字典中删除即可 Being a reference it will automatically removed from the SelectList. 作为参考,它将自动从SelectList中删除。

routingActions.Remove("Disapprove");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM