public ActionResult Save(Book book) { if (!ModelState.IsValid) { var viewModel = new BookViewFormModel(book) { Genres = _context.Genres.ToList() }; ViewBag.Message = book.Id == 0 ? "Create Book" : "Edit Book"; return(View("BookForm", viewModel)); } if (book.Id == 0) { book.DateAdded = DateTime.Today; _context.Books.Add(book); } else { var bookInDb = _context.Books.Single(b => b.Id == book.Id); bookInDb.Name = book.Name; bookInDb.ReleaseDate = book.ReleaseDate; bookInDb.GenreId = book.GenreId; bookInDb.NumberInStock = book.NumberInStock; } _context.SaveChanges(); return(RedirectToAction("Index", "Books")); }
public ActionResult New() { var genres = _context.Genres.ToList(); var viewModel = new BookViewFormModel { Genres = genres }; ViewBag.Message = "Create Book"; return(View("BookForm", viewModel)); }
public ActionResult Edit(int id) { var book = _context.Books.SingleOrDefault(b => b.Id == id); if (book == null) { return(HttpNotFound()); } var viewModel = new BookViewFormModel(book) { Genres = _context.Genres.ToList() }; ViewBag.Message = "Edit Book"; return(View("BookForm", viewModel)); }