예제 #1
0
        public async Task <ActionResult> Index()
        {
            var products = await _productRepository.GetAllAsync();

            var categories = await _categoryRepostitory.GetAllAsync();

            var cart = new ShoppingCart(HttpContext);

            var carts = await cart.GetCartItemsAsync();

            if (carts == null)
            {
                await cart.AddAsync(11);
            }
            else
            {
                await cart.RemoveAsync(11);
            }

            var model = new ProductsViewModel()
            {
                Products   = products,
                CartItems  = carts,
                Categories = categories,
            };

            return(View(viewName: "Index", model: model));
        }
예제 #2
0
        public async Task <ActionResult> AddToCart(int id)
        {
            var cart = new ShoppingCart(HttpContext);

            await cart.AddAsync(id);

            return(RedirectToAction("index"));
        }
예제 #3
0
        //[Route("AddToCart/{nameProduct}")]
        public async Task <ActionResult> AddToCart(ProductsViewModel model)
        //public async Task<JsonResult> AddToCart(string nameProduct)
        {
            var products = await _productRepository.GetAllAsync();

            var product = products.SingleOrDefault(p => p.Name == model.Name && p.Category.Name == model.CategoryName);
            var cart    = new ShoppingCart(HttpContext);


            await cart.AddAsync(product.Id);



            var carts = await cart.GetCartItemsAsync();

            if (carts == null)
            {
                await cart.AddAsync(11);
            }
            else
            {
                await cart.RemoveAsync(11);
            }

            model = new ProductsViewModel()
            {
                Price = product.Price,
                Name  = product.Name,
                //CartItems = carts,
                //Discount = product.Discount,
                //Subtotal = CalcuateCartSubtotal(carts),
                //Total = CalcuateCartWithDiscount(carts),
            };

            //return Json(RedirectToAction("Dashboard","Profile"));
            //return Json(model, JsonRequestBehavior.AllowGet);
            return(RedirectToAction("Index", "Home"));
        }
예제 #4
0
        //[Route("AddToCart/{productId}")]
        //public async Task<ActionResult> AddToCart(ProductsViewModel model)
        public async Task <JsonResult> AddToCart(int productId)
        {
            var cart = new ShoppingCart(HttpContext);
            var user = await GetloggedInUser();


            await cart.AddAsync(productId, user.Id);

            var carts = await cart.GetCartItemsAsync(user.Id);

            var model = new ProductsManagerViewModel
            {
                CartItems              = carts,
                TotalPriceItems        = CalcuateCartSubtotal(carts),
                TotalPriceWithDiscount = CalcuateCartWithDiscount(carts)
            };

            //return Json(RedirectToAction("Index", "Home"));
            return(Json(model, JsonRequestBehavior.AllowGet));
            //return RedirectToAction("Index", "Home");
        }