public RedirectToRouteResult RemoveFromCart(Cart cart,int productId, string returnUrl) { Product product = repository.Products .FirstOrDefault(p => p.ProductID == productId); if (product != null) { cart.RemoveItem(product); } return RedirectToAction("Index", new { returnUrl }); }
public void Can_Remove_Line() { Product p1 = new Product { ProductID = 1, Name = "P1", Price = 100 }; Product p2 = new Product { ProductID = 2, Name = "P2", Price = 100 }; Product p3 = new Product { ProductID = 3, Name = "P3", Price = 100 }; Product p4 = new Product { ProductID = 4, Name = "P4", Price = 100 }; //创建一个购物车 Cart cart = new Cart(); cart.AddItem(p1, 10); cart.AddItem(p2, 2); cart.AddItem(p1, 3); cart.RemoveItem(p2); Assert.AreEqual(cart.Lines.Count(c => c.Product == p2), 0); Assert.AreEqual(cart.Lines.First(p => p.Product==p1).Quantity, 13); }