コード例 #1
0
        public async Task <IActionResult> AddToCart(int id)
        {
            var user = await _userManager.FindByEmailAsync(User.Identity.Name);

            var     cart    = _context.GetCart(user.Id);
            Product product = _context.GetProduct(id);

            var x = _context.AddProductToCart(user, cart, product);

            return(View("Index", cart));
        }
コード例 #2
0
        public async Task <IActionResult> AddToCart(int id)
        {
            ApplicationUser user;

            try
            {
                user = await _userManager.FindByEmailAsync(User.Identity.Name);
            }
            catch
            {
                return(RedirectToAction("Login", "Account"));
            }
            var     cart    = _context.GetCart(user.Id);
            Product product = _context.GetProduct(id);

            var x = _context.AddProductToCart(user, cart, product);

            return(View("Index", cart));
        }
コード例 #3
0
        public async Task <IActionResult> MyCart(int id)
        {
            Product product = _context.Products.First(f => f.ID == id);
            var     user    = await _userManager.FindByEmailAsync(User.Identity.Name);

            Cart cart = _context.Carts.FirstOrDefault(c => c.UserTag == user.Id);

            if (cart == null)
            {
                cart           = new Cart();
                cart.UserTag   = user.Id;
                cart.CartItems = new List <CartItem>();
                await _context.Carts.AddAsync(cart);

                await _context.SaveChangesAsync();
            }
            _cart.AddProductToCart(user, cart, product);
            await _context.SaveChangesAsync();

            return(View(cart));
        }