public ActionResult Create(FormCollection formCollection)
        {
            try {
                Order order = new Order();
                order.ClientID = WebSecurity.CurrentUserId;
                order.Date = DateTime.Now;
                order.ConditionID = db.Conditions.Where(condition => condition.Name.Equals("Оформлен")).FirstOrDefault().ID;
                order.Address = formCollection["address"];
                db.Orders.Add(order);
                db.SaveChanges();
                Cart cart = (Cart)Session["Cart"];
                List<OrderItem> orderItems = new List<OrderItem>();
                foreach (var item in cart.Content) {
                    OrderItem orderItem = new OrderItem();
                    orderItem.BookID = item.Key;
                    orderItem.Count = item.Value;
                    orderItem.OrderID = order.ID;
                    orderItems.Add(orderItem);
                }
                order.OrderItems = orderItems;
                db.Entry(order).State = EntityState.Modified;
                db.SaveChanges();
                ((Cart)Session["Cart"]).Clear();
            } catch {

            }
            return RedirectToAction("Index");
        }
        public ActionResult AddressAndPayment(FormCollection values)
        {
            var order = new Order();
            var cartInfo = ShoppingCart.GetCart(this.HttpContext);
            TryUpdateModel(order);

            try
            {
                    order.UserName = User.Identity.Name;
                    order.OrderDate = DateTime.Now;
                    order.Total = cartInfo.GetTotal();

                    //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.Id });

            }
            catch
            {
                //Invalid - redisplay with errors
                return View(order);
            }
        }
 public IActionResult Create(Order order)
 {
     if (ModelState.IsValid)
     {
         _context.Order.Add(order);
         _context.SaveChanges();
         return RedirectToAction("Index");
     }
     ViewData["CustomerID"] = new SelectList(_context.Set<Customer>(), "CustomerID", "Customer", order.CustomerID);
     return View(order);
 }
 //
 // GET: /Checkout/AddressAndPayment
 public ActionResult AddressAndPayment()
 {
     UserProfile userProfile = storeDB.UserProfiles.SingleOrDefault(u => u.UserName == User.Identity.Name);
     Order order = new Order
     {
         FirstName = userProfile.FirstName,
         LastName = userProfile.LastName,
         Address = userProfile.Adress,
         Town = userProfile.Town,
         Province = userProfile.Province,
         PostalCode = userProfile.PostalCode,
         Country = userProfile.Country,
         PhoneNumber = userProfile.PhoneNumber,
         Email = userProfile.EmailAdress
     };
     return View(order);
 }
Exemple #5
0
        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
                {
                    BookId = item.BookId,
                    OrderId = order.Id,
                    UnitPrice = item.Book.Price,
                    Quantity = item.Count
                };

                // Set the order total of the shopping cart
                orderTotal += (item.Count * item.Book.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.Id;
        }