コード例 #1
0
        public async Task <IActionResult> Create([Bind("UserId,BeerId")] BeerDrank beerDrank)
        {
            if (ModelState.IsValid)
            {
                var beersDrankExists = _context.BeersDrank.FirstOrDefault(wb => wb.UserId == beerDrank.UserId && wb.BeerId == beerDrank.BeerId);
                if (beersDrankExists == null)
                {
                    _context.Add(beerDrank);
                }
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["BeerId"] = new SelectList(_context.Beers, "Id", "Name", beerDrank.BeerId);
            ViewData["UserId"] = new SelectList(_context.Users, "Id", "Email", beerDrank.UserId);
            return(View(beerDrank));
        }
コード例 #2
0
        public IActionResult AddToBeersDrank(int?Id)
        {
            if (Id == null)
            {
                return(NotFound());
            }
            var user            = _context.Users.FirstOrDefault(u => u.UserName == HttpContext.User.Identity.Name);
            var beer            = _context.Beers.FirstOrDefault(b => b.Id == Id);
            var beerDrankExists = _context.BeersDrank.FirstOrDefault(l => l.UserId == user.Id && l.BeerId == Id);

            if (beerDrankExists == null)
            {
                var beerDrank = new BeerDrank
                {
                    UserId = user.Id,
                    User   = user,
                    BeerId = (int)Id,
                    Beer   = beer
                };
                _context.BeersDrank.Add(beerDrank);
                _context.SaveChanges();
            }
            return(Redirect("~/BeersDrank"));
        }