public ActionResult DownloadProcess(FormCollection values) { var download = new Download(); TryUpdateModel(download); try { if (string.Equals(values["PromoCode"], PromoCode, StringComparison.OrdinalIgnoreCase) == false) { return View(download); } else { download.Username = User.Identity.Name; download.DownloadDate = DateTime.Now; //Save Order storeDB.Downloads.Add(download); storeDB.SaveChanges(); //Process the order var cart = DownloadCart.GetCart(this.HttpContext); cart.CreateOrder(download); return RedirectToAction("Complete", new { id = download.DownloadId }); } } catch { //Invalid - redisplay with errors return View(download); } }
/* public decimal GetTotal() { // Multiply album price by count of that album to get // the current price for each of those albums in the cart // sum all album price totals to get the cart total decimal? total = (from cartItems in storeDB.Carts where cartItems.CartId == ShoppingCartId select (int?)cartItems.Count * cartItems.Album.Price).Sum(); return total ?? decimal.Zero; } */ public int CreateOrder(Download download) { //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 DownloadDetail { ChapterId = item.ChapterId, DownloadId = download.DownloadId, //UnitPrice = item.Chapter.Price, Quantity = item.Count }; // Set the order total of the shopping cart //orderTotal += (item.Count * item.Chapter.Price); storeDB.DownloadDetails.Add(orderDetail); } // Set the order's total to the orderTotal count // download.Total = orderTotal; // Save the order storeDB.SaveChanges(); // Empty the shopping cart EmptyCart(); // Return the OrderId as the confirmation number return download.DownloadId; }