public ActionResult AddFoodToCart(String FoodID, String Size) { List <OrderDetail> mycart = (List <OrderDetail>)Session[CART_STRING]; if (mycart == null) { mycart = new List <OrderDetail>(); } OrderDetail item = new OrderDetail { FoodID = FoodID, Size = Size }; if (mycart.Exists(o => o.FoodID == FoodID && o.Size == Size)) { //plus quantity with 1 int index = mycart.FindIndex(o => o.FoodID == item.FoodID && o.Size == item.Size); int oldquantity = mycart[index].Quantity; mycart[index].Quantity = oldquantity + 1; if (DiscountHelper.IsDiscountToday() && oldquantity % 2 == 1) { //discount if (Size == "S") { mycart[index].Discount += mycart[index].Dish.Price * 2 * 0.15; } else if (Size == "M") { mycart[index].Discount += mycart[index].Dish.Price * 2 * 0.25; } else if (Size == "L") { mycart[index].Discount += mycart[index].Dish.Price * 2 * 0.35; } } } else { using (PizzaExpressModel context = new PizzaExpressModel()) { //create new order detail by load some info //Debug.WriteLine("vao den doan lay tu DB"); var dish = context.Dishes.Include(d => d.Food).Single(d => d.FoodID == FoodID && d.Size == Size); //dish.Food = food; item.Dish = dish; item.Size = Size; item.Quantity = 1; mycart.Add(item); } } Session[CART_STRING] = mycart; return(Content("add to cart successfully, size of cart now is " + mycart.Count)); }
protected void gvCart_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName != "Page") { List <OrderDetail> mycart = (List <OrderDetail>)Session[CART_STRING]; int rowindex = ((GridViewRow)((Button)e.CommandSource).NamingContainer).RowIndex; String FoodID = gvCart.Rows[rowindex].Cells[0].Text; String Size = gvCart.Rows[rowindex].Cells[2].Text; int oldquantity = Convert.ToInt32(gvCart.Rows[rowindex].Cells[3].Text); int odindex = mycart.FindIndex(o => o.FoodID == FoodID && o.Size == Size); if (e.CommandName == "PlusQuantity" && oldquantity < 10) { mycart[odindex].Quantity = oldquantity + 1; if (DiscountHelper.IsDiscountToday() && oldquantity % 2 == 1) { //discount if (Size == "S") { mycart[odindex].Discount += mycart[odindex].Dish.Price * 2 * 0.15; } else if (Size == "M") { mycart[odindex].Discount += mycart[odindex].Dish.Price * 2 * 0.25; } else if (Size == "L") { mycart[odindex].Discount += mycart[odindex].Dish.Price * 2 * 0.35; } } } else if (e.CommandName == "MinusQuantity" && oldquantity > 1) { mycart[odindex].Quantity = oldquantity - 1; if (DiscountHelper.IsDiscountToday() && oldquantity % 2 == 0) { //reduce discount if (Size == "S") { mycart[odindex].Discount -= mycart[odindex].Dish.Price * 2 * 0.15; } else if (Size == "M") { mycart[odindex].Discount -= mycart[odindex].Dish.Price * 2 * 0.25; } else if (Size == "L") { mycart[odindex].Discount -= mycart[odindex].Dish.Price * 2 * 0.35; } } } else if (e.CommandName == "RemoveItem") { mycart.RemoveAt(odindex); } loadDataToGridView(); } }