public ActionResult Create(Author author) { try { authorRepository.Add(author); return(RedirectToAction(nameof(Index))); } catch { return(View()); } }
public ActionResult Create(BookAuthorViewModel model) { if (ModelState.IsValid) { string fileName = UploadFile(model.File) == null ? string.Empty : UploadFile(model.File); try { if (model.AuthorId == -1) { // viewbag => Dynamic Properties send data between controller and view // .Message we put the property we need for example we can replace Message by any other name ViewBag.Message = "Please select an author from the list ! "; return(View(GetAllAuthors())); } var author = authorRepository.Find(model.AuthorId); Book book = new Book(); book.Id = model.BookId; book.Title = model.Title; book.Description = model.Description; book.Author = author; book.ImageUrl = fileName; bookRepository.Add(book); return(RedirectToAction(nameof(Index))); } catch { return(View()); } } ModelState.AddModelError("", "You have to fill all required fields"); return(View(GetAllAuthors())); }