Example #1
0
        public ActionResult AddBook(Book book)
        {
            Author author = null;

            author = authorManager.GetAuthorByID(book.AuthorID);
            Publisher publisher = null;

            publisher = publisherManager.GetPublisherByID(book.Publisher);

            string authorIDText = Request.Form["AuthorID"];
            int    authorID     = 0;

            int.TryParse(authorIDText, out authorID);
            book.AuthorID = authorID;
            try
            {
                UpdateModel <Book>(book);
            }
            catch (Exception) { /* book model cannot be updated */ }

            if (ModelState.IsValid)
            {
                if (bookManager.CheckISBN(book.ISBN))
                {
                    ModelState.AddModelError("", "ISBN already exists");
                    return(View());
                }
                else if (author == null)
                {
                    ModelState.AddModelError("", "Author does not exist");
                    return(View());
                }
                else if (publisher == null)
                {
                    ModelState.AddModelError("", "Publisher does not exist");
                    return(View());
                }
                else
                {
                    bookManager.AddNewBook(book);
                    return(RedirectToAction("Details", "Book", book));
                }
            }
            else
            {
                return(View());
            }
        }