public ActionResult Edit(int?id, string returnUrl) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } var book = db.Books.Find(id); if (book == null) { return(HttpNotFound()); } var model = new AddAuthorToBookModel() { Book = book, Authors = db.Authors.ToList(), SelectedAuthor = -1, Publishers = db.Publishers.ToList(), SelectedPublisher = -1, Genres = db.Genres.ToList(), SelectedGenre = -1, }; ViewBag.ReturnUrl = returnUrl; return(View(model)); }
public ActionResult Create(string returnUrl) { var model = new AddAuthorToBookModel() { Book = new Book(), Authors = (db.Authors.ToList()), SelectedAuthor = -1, Publishers = (db.Publishers.ToList()), SelectedPublisher = -1, Genres = (db.Genres.ToList()), SelectedGenre = -1 }; ViewBag.ReturnUrl = returnUrl; return(View(model)); }
public ActionResult Edit(AddAuthorToBookModel model, string returnUrl) { if (ModelState.IsValid) { var book = db.Books.Find(model.Book.Id); if (book == null) { return(HttpNotFound()); } book.Title = model.Book.Title; book.Pages = model.Book.Pages; book.Quantity = model.Book.Quantity; book.CoverURL = model.Book.CoverURL; book.PublishPlace = model.Book.PublishPlace; book.PublishDate = model.Book.PublishDate; book.Price = model.Book.Price; var author = db.Authors.Find(model.SelectedAuthor); if (author == null) { model = new AddAuthorToBookModel() { Book = new Book(), Authors = (db.Authors.ToList()), SelectedAuthor = -1, Publishers = (db.Publishers.ToList()), SelectedPublisher = -1, Genres = (db.Genres.ToList()), SelectedGenre = -1 }; ViewBag.ReturnUrl = returnUrl; ViewBag.ErrorAuthor = "Author does not exists!"; return(View(model)); } book.Authors.Add(db.Authors.Find(model.SelectedAuthor)); var publisher = db.Publishers.Find(model.SelectedPublisher); if (publisher == null) { model = new AddAuthorToBookModel() { Book = new Book(), Authors = (db.Authors.ToList()), SelectedAuthor = -1, Publishers = (db.Publishers.ToList()), SelectedPublisher = -1, Genres = (db.Genres.ToList()), SelectedGenre = -1 }; ViewBag.ReturnUrl = returnUrl; ViewBag.ErrorPublisher = "Publisher does not exists!"; return(View(model)); } book.Publishers.Add(db.Publishers.Find(model.SelectedPublisher)); var genre = db.Genres.Find(model.SelectedGenre); if (genre == null) { model = new AddAuthorToBookModel() { Book = new Book(), Authors = (db.Authors.ToList()), SelectedAuthor = -1, Publishers = (db.Publishers.ToList()), SelectedPublisher = -1, Genres = (db.Genres.ToList()), SelectedGenre = -1 }; ViewBag.ReturnUrl = returnUrl; ViewBag.ErrorGenre = "Genre does not exists!"; return(View(model)); } book.Genre = genre; db.SaveChanges(); return(RedirectToLocal(returnUrl)); } model = new AddAuthorToBookModel() { Book = db.Books.Find(model.Book.Id), Authors = db.Authors.ToList(), Publishers = db.Publishers.ToList(), Genres = db.Genres.ToList(), }; ViewBag.ReturnUrl = returnUrl; return(View(model)); }