コード例 #1
0
        public async Task <IActionResult> Create([Bind("BrandId,BrandName")] Brand brand)
        {
            if (ModelState.IsValid)
            {
                _context.Add(brand);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(brand));
        }
コード例 #2
0
        public async Task <IActionResult> Create([Bind("BranchId,BranchName,Address,Latitude,Longitude")] Branch branch)
        {
            if (ModelState.IsValid)
            {
                _context.Add(branch);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(branch));
        }
コード例 #3
0
        public async Task <IActionResult> Create([Bind("OrderId,OrderDate,UserId")] Order order)
        {
            order.User = _context.Users.SingleOrDefault(u => u.UserId == order.UserId);

            if (ModelState.IsValid)
            {
                _context.Add(order);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(order));
        }
コード例 #4
0
        public async Task <IActionResult> AddToCart(int productId, string returnUrl)
        {
            User user = await _context.Users.Include(up => up.UserProducts).FirstAsync(u => u.Email.Equals(User.FindFirstValue(ClaimTypes.Email)));

            Product productToAdd = await _context.Products.Include(b => b.Brand).FirstAsync(p => p.ProductId == productId);

            foreach (UserProduct item in user.UserProducts)
            {
                if (item.ProductId == productId)
                {
                    return(RedirectToAction("Catalog", "Home"));
                }
            }

            if (productToAdd != null)
            {
                if (user.UserProducts == null)
                {
                    user.UserProducts = new List <UserProduct>();
                }

                user.UserProducts.Add(new UserProduct()
                {
                    ProductId = productToAdd.ProductId,
                    Product   = productToAdd,
                    UserId    = user.UserId,
                    User      = user
                });

                _context.Update(user);
                await _context.SaveChangesAsync();
            }

            return(RedirectToAction("Catalog", "Home"));
        }
コード例 #5
0
        public async Task <IActionResult> DeleteFromCart(int productId)
        {
            User user = await _context.Users
                        .Include(u => u.UserProducts)
                        .ThenInclude(p => p.Product)
                        .FirstAsync(u => u.Email.Equals(User.FindFirstValue(ClaimTypes.Email)));

            foreach (UserProduct up in user.UserProducts)
            {
                if (up.ProductId == productId)
                {
                    user.UserProducts.Remove(up);
                    break;
                }
            }

            _context.Update(user);
            await _context.SaveChangesAsync();

            return(RedirectToAction(nameof(GetMyCart)));
        }