public ActionResult AddressAndPayment(FormCollection values) { var order = new Order(); TryUpdateModel(order); try { // Check if the payment was valid (if values don't equal promocode.. else save order) if (string.Equals(values["PromoCode"], PromoCode, StringComparison.OrdinalIgnoreCase) == false) { return View(order); } else { order.Username = User.Identity.Name; order.OrderDate = DateTime.Now; // Save the order db.Orders.Add(order); db.SaveChanges(); // Process order var cart = ShoppingCart.GetCart(this.HttpContext); cart.CreateOrder(order); return RedirectToAction("Complete", new { id = order.OrderId }); } } catch { // Invalid return View(); } }
public int CreateOrder(Order order) { decimal orderTotal = 0; var cartItems = GetCartItems(); // Iterate over the items in the cart, // adding the order details for each foreach (var item in cartItems) { var orderDetail = new OrderDetail { AlbumId = item.TeaId, OrderId = order.OrderId, UnitPrice = item.Tea.Price, Quantity = item.Count }; // Set the order total of the shopping cart orderTotal += (item.Count * item.Tea.Price); db.OrderDetails.Add(orderDetail); } // Set the order's total to the orderTotal count order.Total = orderTotal; // Save the order db.SaveChanges(); // Empty the shopping cart EmptyCart(); // Return the OrderId as the confirmation number return order.OrderId; }