public ActionResult AddBookAuthor(int id) { LibraryManagementSystemContext context = new LibraryManagementSystemContext(); BooksRepository booksRepository = new BooksRepository(context); AuthorsRepository authorsRepository = new AuthorsRepository(context); BooksAddBookAuthorVM model = new BooksAddBookAuthorVM(); Book book = booksRepository.GetByID(id); model.ID = book.ID; model.Title = book.Title; model.Authors = model.Authors ?? new List <SelectListItem>(); model.Authors = SelectListHandler.Create <Author>( authorsRepository.GetAll(), a => (a.FirstName + " " + a.LastName), a => a.ID.ToString(), model.AuthorID.ToString()); return(View(model)); }
public ActionResult AddBookAuthor(BooksAddBookAuthorVM model) { LibraryManagementSystemContext context = new LibraryManagementSystemContext(); BooksRepository booksRepository = new BooksRepository(context); AuthorsRepository authorsRepository = new AuthorsRepository(context); Book book = null; Author author = null; if (!ModelState.IsValid) { return(View(model)); } else { book = booksRepository.GetByID(model.ID); author = authorsRepository.GetByID(model.AuthorID); book.Authors.Add(author); booksRepository.Save(book); } return(RedirectToAction("Details/" + model.ID, "Books")); }