コード例 #1
0
        public ActionResult AddToCart(FormCollection col)
        {
            string productID = col["id"];
            int    price     = Convert.ToInt32(col["price"]);

            Order        cart     = Session["CART"] as Order;
            Account      acc      = Session["Account"] as Account;
            CartDAO      cartDao  = new CartDAO();
            List <Order> cartList = cartDao.GetAllCarts();
            string       idCart   = (cartList.Count + 1).ToString();

            if (cart == null)
            {
                string address       = col["address"];
                string cartName      = "Cart of " + acc.FullName;
                string payment       = "COD";
                string paymentStatus = "Waiting";
                cart = new Order(idCart, cartName, acc.Username, DateTime.Now, paymentStatus, payment, address, 1);
            }

            List <OrderDetail> OrderList = cart.detailList;
            bool isFound = false;

            for (int i = 0; i < OrderList.Count; i++)
            {
                if (OrderList[i].IDProduct.Equals(productID))
                {
                    OrderList[i].Quantity += 1;
                    isFound = true;
                }
            }
            if (!isFound)
            {
                OrderDetail detail = new OrderDetail(productID, idCart, 1, price);
                cart.detailList.Add(detail);
            }
            int total = 0;

            for (int i = 0; i < cart.detailList.Count; i++)
            {
                total += (cart.detailList[i].Price * cart.detailList[i].Quantity);
            }
            cart.Total      = total;
            Session["CART"] = cart;

            return(Redirect(Url.Action("GetDetail1", "Phone", new { id = productID })));
        }