public IActionResult EditBook(int?id) //It is a short-cut way to write Nullable<int> "?" { ViewBag.deleteBookId = id; // need the Id from the database, so I can add the option in the EditBook -View to delete the book if (id == null) { ViewBag.ErrorMessage = $"Book with the respective ID:{id} cannot be found."; return(View("NotFound")); } //Get the book from the database BooksDisplayed book = _bookStore.GetSpecificBook(id); //Check for the book if (book == null) { ViewBag.ErrorMessage = $"Book with the respective ID:{id} cannot be found."; return(View("NotFound")); } EditBookViewModels model = new EditBookViewModels() { Id = book.BookId, BooksInStore = book.BooksInStore, BookGenre = book.BookGenre, Price = book.Price, StockOfBooks = book.StockOfBooks }; return(View(model)); }
public IActionResult EditBook(EditBookViewModels model) { //Get the book from the database BooksDisplayed book = _bookStore.GetSpecificBook(model.Id); //Check for the book if (book == null) { ViewBag.ErrorMessage = $"Book with the respective ID:{model.Id} cannot be found."; return(View("NotFound")); } if (ModelState.IsValid) { book.BooksInStore = model.BooksInStore; book.BookGenre = model.BookGenre; book.StockOfBooks = model.StockOfBooks; book.Price = model.Price; _bookStore.UpdateBook(book); } return(View(model)); }