Remove item from SelectList in C#
Asked Answered
C

3

5

I have a method in which return me selectList. The certain items of selectList are to be programatically removed on the basis of 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". Could any one kindly help me to solve this.

Circumscribe answered 12/6, 2015 at 12:35 Comment(4)
routingActions.Remove("Disapprove")?Izolaiztaccihuatl
The question is not clear. Maybe what you want is removing something from routingActions and not form the selectListBerriman
The above method returns the selectList to the controller , inside controller I might need to remove certain item. @BerrimanCircumscribe
@Circumscribe then see my answer, i explained how you can do thatEdmee
E
16

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:

public class SomeController
{

   public ActionResult Index()
   {

       SelectList list = GetRoutingActionList();

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

        return View();
   }

}
Edmee answered 12/6, 2015 at 12:41 Comment(2)
Actually GetRoutingActionList() is common method that is called from other controller , and the item from returned selectlist is removed as per requirement in he controller @EhsanCircumscribe
you can use the above code in controller or view wherever you need to remove itemEdmee
B
3
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();
}
Brannan answered 12/6, 2015 at 12:48 Comment(0)
R
2

Simply remove it from dictionary. Being a reference it will automatically removed from the SelectList.

routingActions.Remove("Disapprove");
Rexferd answered 12/6, 2015 at 12:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.