public ActionResult AddressAndPayment(FormCollection form) { Order order = new Order(); TryUpdateModel(order); try { order.OrderDate = DateTime.Now; // Save Order storeDB.Orders.Add(order); storeDB.SaveChanges(); // Process order ShoppingCart cart = new ShoppingCart(this.HttpContext); cart.CreateOrder(order); return RedirectToAction("Complete", new { id = order.OrderId }); } catch { return View(order); } }
public void 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 { BookIsbn = item.BookIsbn, OrderId = order.OrderId, UnitPrice = item.Book.BookPrice, Quantity = item.Count }; // Set the order total of the shopping cart orderTotal += (item.Count * item.Book.BookPrice); storeDB.OrderDetails.Add(orderDetail); } // set the order's total to the orderTotal count order.Total = orderTotal; // save the order storeDB.SaveChanges(); // Empty the shopping cart EmptyCart(); }