public ActionResult AddressAndPayment(FormCollection values) { var order = new OrderModel(); TryUpdateModel(order); try { if (string.Equals(values["PromoCode"], PromoCode, StringComparison.OrdinalIgnoreCase) == false) { return View(order); } else { order.Username = User.Identity.Name; order.OrderDate = DateTime.Now; //Save Order storeDB.Orders.Add(order); storeDB.SaveChanges(); //Process the order var cart = ShoppingCart.GetCart(this.HttpContext); cart.CreateOrder(order); return RedirectToAction("Complete", new { id = order.OrderId }); } } catch { //Invalid - redisplay with errors return View(order); } }
// GET: /Checkout/AddressAndPayment public ActionResult AddressAndPayment() { var username = User.Identity.Name; var context = new GadgetEntities(); var id = WebSecurity.CurrentUserId; var user = context.UserProfile.SingleOrDefault(u => u.UserId == id); var order = new OrderModel(); order.LastName = user.LastName; order.FirstName = user.FirstName; order.Email = user.EmailAddress; order.City = user.City; order.Country = user.Country; order.Address = user.Address; return View(order); }
public int CreateOrder(OrderModel 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 OrderDetailModel { ItemId = item.ItemId, OrderId = order.OrderId, UnitPrice = item.Item.Price, Quantity = item.Count }; // Set the order total of the shopping cart orderTotal += (item.Count * item.Item.Price); 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; }