public ActionResult EditBook(int id) { LibraryManagementSystemContext context = new LibraryManagementSystemContext(); BooksRepository booksRepository = new BooksRepository(context); PublishersRepository publishersRepository = new PublishersRepository(context); BooksEditBookVM model = new BooksEditBookVM(); Book book = booksRepository.GetByID(id); if (id > 0) { if (book == null) { return(RedirectToAction("Index", "Books")); } model.ID = book.ID; model.Title = book.Title; model.PublisherID = book.Publisher.ID; model.StockCount = book.StockCount; model.DeliveryPrice = book.DeliveryPrice; model.DateReceived = book.DateReceived; model.DatePublished = book.DatePublished; } else { book = new Book(); } model.Publishers = model.Publishers ?? new List <SelectListItem>(); model.Publishers = SelectListHandler.Create <Publisher>( publishersRepository.GetAll(), p => p.Name, p => p.ID.ToString(), model.PublisherID.ToString()); return(View(model)); }
public ActionResult EditBook(BooksEditBookVM model) { LibraryManagementSystemContext context = new LibraryManagementSystemContext(); BooksRepository booksRepository = new BooksRepository(context); PublishersRepository publishersRepository = new PublishersRepository(context); Book book = null; if (!ModelState.IsValid) { model.Publishers = model.Publishers ?? new List <SelectListItem>(); model.Publishers = SelectListHandler.Create <Publisher>( publishersRepository.GetAll(), p => p.Name, p => p.ID.ToString(), model.PublisherID.ToString()); return(View(model)); } else { if (model.ID > 0) { book = booksRepository.GetByID(model.ID); } else { book = new Book(); } book.ID = model.ID; book.Title = model.Title; book.PublisherID = model.PublisherID; book.StockCount = model.StockCount; book.DeliveryPrice = model.DeliveryPrice; book.DateReceived = model.DateReceived; book.DatePublished = model.DatePublished; booksRepository.Save(book); } return(RedirectToAction("Index", "Books")); }