public RedirectToRouteResult AddToCart(CartViewModel cart, int productId, string returnUrl)
 {
     using (InternetStoreDBContext dbc = new InternetStoreDBContext())
     {
         var product = (from item in dbc.Products where item.ID == productId select item).ToList().FirstOrDefault();
         if (product != null)
         {
             cart.AddItem(product, 1);
         }
     }
     return RedirectToAction("Cart", new { returnUrl });
 }
        public ViewResult Checkout(CartViewModel cart, User user, Order order)
        {
            if (cart.OrdersDetails.Count() == 0)
            {
                ViewBag.IsSuccess = false;
                ViewBag.Message = "Sorry, your cart is empty!";
            }
            else
            {
                ViewBag.IsSuccess = true;
                ViewBag.Message = "Thanks for placing your order. We'll ship your goods as soon as possible.";

                using (InternetStoreDBContext dbc = new InternetStoreDBContext())
                {
                    if (!User.Identity.IsAuthenticated)
                    {
                        dbc.Users.InsertOnSubmit(user);
                        dbc.SubmitChanges();
                        order.UserID = user.ID;
                    }
                    else
                    {
                        User currentUser = (from u in dbc.Users where u.Email == User.Identity.Name select u).ToList().FirstOrDefault();
                        order.UserID = currentUser.ID;
                    }

                    order.ShippingDate = DateTime.Now.ToOADate();
                    if (order.ShippingStatus == null)
                        order.ShippingStatus = "Check";
                    dbc.Orders.InsertOnSubmit(order);
                    dbc.SubmitChanges();

                    Sale sale = new Sale() { OrderID = order.ID, SalesAmount = cart.ComputeTotalValue() };
                    dbc.Sales.InsertOnSubmit(sale);
                    dbc.SubmitChanges();

                    foreach (var orderDetails in cart.OrdersDetails)
                    {
                        var orderDetails2 = new OrderDetails() { OrderID = order.ID, ProductID = orderDetails.ProductID, Quantity = orderDetails.Quantity };
                        dbc.OrderDetails.InsertOnSubmit(orderDetails2);
                        dbc.SubmitChanges();
                    }

                    cart.Clear();
                }
            }

            ViewBag.IsAuthenticated = User.Identity.IsAuthenticated;
            return View();
        }
 public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
 {
     CartViewModel cart = null;
     if (controllerContext.HttpContext.Session != null)
     {
         cart = (CartViewModel)controllerContext.HttpContext.Session[sessionKey];
     }
     if (cart == null)
     {
         cart = new CartViewModel();
         if (controllerContext.HttpContext.Session != null)
         {
             controllerContext.HttpContext.Session[sessionKey] = cart;
         }
     }
     return cart;
 }
        public ViewResult Cart(CartViewModel cart, string returnUrl)
        {
            ViewBag.ReturnUrl = returnUrl;

            return View(cart);
        }
 private CartViewModel GetCart()
 {
     CartViewModel cart = (CartViewModel)Session["Cart"];
     if (cart == null)
     {
         cart = new CartViewModel();
         Session["Cart"] = cart;
     }
     return cart;
 }