public async Task <IActionResult> Edit(string id, [Bind("Id,GenreId,BookId")] BooksGenres booksGenres)
        {
            if (id != booksGenres.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(booksGenres);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!BooksGenresExists(booksGenres.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            ViewData["BookId"]  = new SelectList(_context.Book, "Id", "Id", booksGenres.BookId);
            ViewData["GenreId"] = new SelectList(_context.Genre, "Id", "Id", booksGenres.GenreId);
            return(View(booksGenres));
        }
Beispiel #2
0
 /// <summary>
 /// Adds a new book-genre relation.
 /// </summary>
 /// <param name="bookId"></param>
 /// <param name="genreId"></param>
 public void Add(int bookId, int genreId)
 {
     using (libraryContext = generator.Generate())
     {
         BooksGenres booksGenre = new BooksGenres();
         booksGenre.BookId  = bookId;
         booksGenre.GenreId = genreId;
         libraryContext.BooksGenres.Add(booksGenre);
         libraryContext.SaveChanges();
     }
 }
        public async Task <IActionResult> Create([Bind("Id,GenreId,BookId")] BooksGenres booksGenres)
        {
            if (ModelState.IsValid)
            {
                _context.Add(booksGenres);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["BookId"]  = new SelectList(_context.Book, "Id", "Id", booksGenres.BookId);
            ViewData["GenreId"] = new SelectList(_context.Genre, "Id", "Id", booksGenres.GenreId);
            return(View(booksGenres));
        }