Beispiel #1
0
        public ActionResult AddCart(FormCollection f, string strURL)
        {
            try
            {
                if (f == null)
                {
                    RedirectToAction("Index", "Error");
                    return null;
                }
                int productId = int.Parse(f["productID"].ToString());
                string productImage = f["productImage"].ToString();
                int productNumber = int.Parse(f["txtQuantity"].ToString());
                List<CustomerCartViewModel> lstCart = GetCart();
                CustomerCartViewModel product = lstCart.Find(n => n.ProductId == productId);
                if (product == null)
                {
                    product = new CustomerCartViewModel(productId);
                    product.ProductId = productId;
                    product.ProductImage = productImage;
                    product.Quantity = productNumber;
                    product.Total = product.Price * product.Quantity;
                    lstCart.Add(product);
                    ViewBag.lstProductCart = lstCart;
                }
                else
                {
                    product.Quantity = productNumber;
                    product.Total = product.Price * product.Quantity;
                    ViewBag.lstProductCart = lstCart;
                    return Redirect(strURL);
                }

                return Redirect(strURL);
            }
            catch (Exception)
            {
                return RedirectToAction("Index", "Error");
            }
        }
 public List<CustomerCartViewModel> GetOrderToCart(int orderId)
 {
     List<CustomerCartViewModel> lstCart = new List<CustomerCartViewModel>();
     Order order = db.Orders.SingleOrDefault(n => n.OrderId == orderId);
     var orderItems = db.OrderItems.Where(n => n.OrderId == orderId).ToList();
     if (lstCart != null)
     {
         for (int i = 0; i < orderItems.Count; i++)
         {
             CustomerCartViewModel cart = lstCart.Find(n => n.ProductId == orderItems[i].ProductId);
             if (cart == null)
             {
                 cart = new CustomerCartViewModel(orderItems[i].ProductId);
                 cart.ProductId = orderItems[i].ProductId;
                 cart.ProductImage = orderItems[i].Product.ProductImage;
                 cart.ProductName = orderItems[i].Product.ProductName;
                 cart.Quantity = orderItems[i].Quantity;
                 cart.Price = orderItems[i].RealPrice;
                 cart.Total = orderItems[i].Amount;
                 lstCart.Add(cart);
             }
         }
     }
     return lstCart;
 }