Exemplo n.º 1
0
 public ActionResult Index(Cart cart,string returnUrl)
 {
     return View(new CartIndexViewModel {
         Cart = cart,
         ReturnUrl = returnUrl
     });
 }
Exemplo n.º 2
0
 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 });
 }
Exemplo n.º 3
0
        public RedirectToRouteResult AddToCart(Cart cart,int productId, string returnUrl)
        {
            var product = repository.Products
                .FirstOrDefault(p => p.ProductID == productId);

            if (product != null) {
                cart.AddItem(product, 1);
            }

            return RedirectToAction("Index", new { returnUrl });
        }
Exemplo n.º 4
0
        public ViewResult Checkout(Cart cart, ShippingDetails shippingDetails)
        {
            if (!cart.Lines.Any())
                ModelState.AddModelError("", "Sorry, your cart is empty!");

            if (ModelState.IsValid) {
                orderProcessor.ProcessOrder(cart, shippingDetails);
                cart.ClearItem();
                return View("Completed");
            }

            return View(shippingDetails);
        }
Exemplo n.º 5
0
 public object BindModel(ControllerContext controllerContext,
 ModelBindingContext bindingContext)
 {
     // get the Cart from the session
     Cart cart = (Cart)controllerContext.HttpContext.Session[sessionKey];
     // create the Cart if there wasn't one in the session data
     if (cart == null) {
         cart = new Cart();
         controllerContext.HttpContext.Session[sessionKey] = cart;
     }
     // return the cart
     return cart;
 }
Exemplo n.º 6
0
        public void Calculate_Cart_Total()
        {
            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);

            Assert.AreEqual(cart.ComputeTotalValue(), 1200);
        }
Exemplo n.º 7
0
        public void Can_Add_New_Lines()
        {
            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);
            var result = cart.Lines.ToArray();
            Assert.AreEqual(result.Length, 2);
            Assert.AreEqual(result[0].Product, p1);
            Assert.AreEqual(result[1].Product, p2);
        }
Exemplo n.º 8
0
        public void Can_Clear_Contents()
        {
            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.ClearItem();

            Assert.AreEqual(cart.Lines.Count(), 0);
        }
Exemplo n.º 9
0
 public PartialViewResult Summary(Cart cart)
 {
     return PartialView(cart);
 }
Exemplo n.º 10
0
        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);
        }