public ActionResult AddressAndPayment(FormCollection values) { var order = new Order(); TryUpdateModel(order); try { if (string.Equals(values["Promocode"], PROMOCODE, StringComparison.OrdinalIgnoreCase) == false) // { return View(order); } //start processing else { order.Username = User.Identity.Name; order.OrderDate = System.DateTime.Now; order.Country = "USA"; //default to US for now storeDB.Orders.Add(order); // add order first storeDB.SaveChanges(); //order detail var cart = ShoppingCart.GetCart(this.HttpContext); int orderID = cart.CreateOrder(order); return RedirectToAction("Complete", new { id = orderID }); } } catch { //Invalid - rediplay order with errors return View(order); } }
//CreateOrder converts the shopping cart to an order during the checkout phase. public int CreateOrder(Order order) { decimal orderTotal = 0; var cartItems = GetCartItems(); foreach (var c in cartItems) { var orderDetail = new OrderDetail { AlbumID=c.AlbumID , OrderID=order.OrderID , UnitPrice=c.Album.Price , Quantity=c.Count }; // Set the order total of the shopping cart orderTotal += (c.Album.Price * c.Count); 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(); // Return the OrderId as the confirmation number return order.OrderID; }