public ActionResult Store(Book book)
        {
            if (!ModelState.IsValid)
            {
                ViewBag.Book = book;
                return(View("Create"));
            }

            var dao = new BookDAO();

            dao.Create(book);
            return(RedirectToAction("Index"));
        }
Esempio n. 2
0
        public ActionResult Create(Book book, string[] selectedAuthors)
        {
            PopulateLanguagesDropDownList();
            PopulateGenresDropDownList();
            PopulateBookAuthors(book);
            try
            {
                if (ModelState.IsValid)
                {
                    Language l = languageDAO.Find(book.LanguageID);
                    Genre    g;
                    if (book.GenreID == -1)
                    {
                        g      = new Genre();
                        g.Id   = -1;
                        g.Name = "";
                    }
                    else
                    {
                        g = genreDAO.Find(book.GenreID);
                    }
                    book.Id       = dao.CurrentID() + 1;
                    book.Language = l;
                    book.Genre    = g;
                    UpdateBookAuthors(selectedAuthors, book);
                    if (book.Authors.Count == 0)
                    {
                        ModelState.AddModelError("", "At least one author should be selected");
                        return(View(book));
                    }
                    dao.Create(book);
                    return(RedirectToAction("Index"));
                }
            }
            catch
            {
                ModelState.AddModelError("", "Unable to save changes.");
            }

            return(View(book));
        }