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)); }
public ActionResult GetContact(String name, String email, String phone, String message) { using (PizzaExpressModel context = new PizzaExpressModel()) { object[] contactparams = new object[] { new SqlParameter("@name", name), new SqlParameter("@email", email), new SqlParameter("@phone", phone), new SqlParameter("@message", message) }; context.Database.ExecuteSqlCommand("insert into Contact values(@name, @email, @phone, @message)", contactparams); } return(Redirect("/Home/")); }
protected void btnOrder_Click(object sender, EventArgs e) { List <OrderDetail> mycart = (List <OrderDetail>)Session[CART_STRING]; if (mycart != null) { //add order and order detail to database String name = tbxName.Text.Trim(); String address = tbxAddress.Text.Trim(); String phone = tbxPhone.Text.Trim(); String note = tbxNote.Text.Trim(); DateTime shippedDate = Convert.ToDateTime(tbxShipdate.Text); using (PizzaExpressModel context = new PizzaExpressModel()) { object[] dbparams = new object[] { new SqlParameter("@name", name), new SqlParameter("@address", address), new SqlParameter("@phone", phone), new SqlParameter("@note", note), new SqlParameter("@orderDate", DateTime.Now), new SqlParameter("@shippedDate", shippedDate) }; int OrderID = context.Database.SqlQuery <int>("insert into Orders(CustomerName, CustomerAddress, " + "Phone, OrderDate, ShippedDate, Note) output inserted.OrderID values(@name, @address, @phone, @orderDate, @shippedDate, @note)", dbparams).Single(); foreach (var od in mycart) { object[] odparams = new object[] { new SqlParameter("@OrderID", OrderID), new SqlParameter("@FoodID", od.FoodID), new SqlParameter("@Size", od.Size), new SqlParameter("@Discount", od.Discount), new SqlParameter("@Quantity", od.Quantity) }; var x = context.Database.ExecuteSqlCommand("insert into OrderDetail values(@OrderID, @FoodID, @Size, @Discount, @Quantity)", odparams); } mycart.Clear(); Session[CART_STRING] = mycart; Response.Redirect("/Home/Index"); } } }
// GET: CustomerOrder public ActionResult Index(int?CategoryID, int?page) { using (PizzaExpressModel context = new PizzaExpressModel()) { var Categories = context.Categories.ToList(); ViewData["Categories"] = Categories; Category Category = null; var pageindex = page ?? 1; var Food = context.Foods.Include(f => f.Dishes) .Where(f => f.CategoryID == (CategoryID == null ? 1 : CategoryID)) .OrderBy(f => f.FoodID) .ToPagedList(pageindex, PAGESIZE); Category = context.Categories.Single (c => c.CategoryID == (CategoryID == null ? 1 : CategoryID)); ViewData["Category"] = Category; return(View(Food)); } }