如何删除列表中的项目列表?

时间:2018-12-12 12:02:25

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

我正在使用asp.net MVC应用程序,我有一个包含(ProductName,Price);

的Products表中选定项目的列表。

我有2个按钮,其中一个可以清除一项(工作中)

DeleteOneItem部分视图:

@using (Ajax.BeginForm("DeleteItemCheckbox", "Home", new AjaxOptions() { 
 UpdateTargetId = "div_print", InsertionMode = InsertionMode.Replace }))
        {
    <input type="number" name="ProductId" hidden value="@item.ProductId" />
    <div>Product Name :   @item.ProductName    </div>
    <p>Product Price :  @item.ProductPrice $  </p>
    <input type="submit"class="btn btn-primary btn-info" value="Delete Item" /><br />
}

DeleteOneItem方法:

 public ActionResult DeleteItem(int ProductId)
    {

        List<Product> AddedToCart = new List<Product>();
        AddedToCart = (List<Product>)Session["listOfSelectedItems"];

        var deleteSelecedItem = AddedToCart.Where(a => a.ProductID.Equals(pId)).FirstOrDefault();

        AddedToCart.Remove(deleteSelecedItem);

        var viewModel = new MyViewModel
        {
            itemsNumbers = Convert.ToInt32(Session["items"]),
            SumVM = Convert.ToInt32(Session["sum"]),
            AddToCartList = AddedToCart
        };

        return View("Cart", viewModel);
    }

另一个删除选定项目的项目,但我不知道该怎么做。

我首先通过添加所有选定的项目到列表中 每个元素旁边的检查输入。

<input class="form-check-input" type="checkbox" value="@item.ProductId" 
id="defaultCheck1" name="employeeIdsToDelete">
 ....
 ....
 ....
 <input type="submit"class="btn btn-primary btn-info" value="Delete Selected  Items" /><br />

然后在控制器中,我可以获取所有选定项目ID的列表

public ActionResult DeleteItemCheckbox(List<int> employeeIdsToDelete)
    {
     var ListP=employeeIdsToDelete.ToList();
     ..

现在,我已经有了一个名为ItemsSelectedToBuy的列表,其中包含用户想要购买的所选产品。 在Session [“ listOfSelectedItems”];

我要删除的是在employeeIdsToDelete中选择的所有项目 来自Session [“ listOfSelectedItems”],这是所选产品的列表。

4 个答案:

答案 0 :(得分:0)

public ActionResult DeleteItemCheckbox(List<int> employeeIdsToDelete)
{
    var ListP = employeeIdsToDelete.ToList();

    foreach (var item in ListP)
    {
        (List<Product>)Session["listOfSelectedItems"].RemoveAll(x => x.ID == item);
    }
}

答案 1 :(得分:0)

List<Product> AddedToCart = new List<Product>();
AddedToCart = (List<Product>)Session["listOfSelectedItems"];

foreach (int item in employeeIdsToDelete)
{
     employeeIdsToDelete.RemoveAll(X => X.ProductID == item);
}

答案 2 :(得分:0)

    public ActionResult DeleteItemCheckbox(List<int> employeeIdsToDelete)
    {
            var ListP = employeeIdsToDelete.ToList();
            List<int> listOfSelectedItems = (List<int>)Session["listOfSelectedItems"];
            List<int> finallist = listOfSelectedItems
                                  .Where(x => !employeeIdsToDelete.Contains(x))
                                  .ToList();
            Session["listOfSelectedItems"] = finallist;
    }

答案 3 :(得分:0)

它像这样工作。

 public ActionResult DeleteItemCheckbox(List<int> employeeIdsToDelete)
    {

        List<Product> AddedToCart = new List<Product>();
        AddedToCart = (List<Product>)Session["listOfSelectedItems"];

        var deleteSelecedItem = AddedToCart.Where(a => 
         a.ProductID.Equals(employeeIdsToDelete)).FirstOrDefault();

        List<Product> finallist = AddedToCart
                              .Where(x => !employeeIdsToDelete.Contains(x.ProductID))
                              .ToList();