public ActionResult Edit(int?id, int page, string orderBy, string search, string bookGenre)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            ViewBag.Page      = page;
            ViewBag.OrderBy   = orderBy;
            ViewBag.Search    = search;
            ViewBag.BookGenre = bookGenre;

            Book book = db.Query <Book>().FirstOrDefault(b => b.BookId == id);

            if (book == null)
            {
                return(HttpNotFound());
            }

            var model = new AddAuthorToBook()
            {
                Book             = new Book(),
                Authors          = db.Query <Author>().ToList(),
                SelectedAuthorId = book.Author.AuthorId,
                SelectedBookId   = -1
            };

            model.Book = book;
            return(View(model));
        }
        public ActionResult Create()
        {
            var model = new AddAuthorToBook()
            {
                Book             = new Book(),
                Authors          = db.Query <Author>().ToList(),
                SelectedAuthorId = -1,
                SelectedBookId   = -1
            };

            return(View(model));
        }
        public ActionResult CreateFromRequest(Request request)
        {
            Book newBook = new Book
            {
                Name = request.Title,
            };
            var model = new AddAuthorToBook()
            {
                Book             = newBook,
                Authors          = db.Query <Author>().ToList(),
                SelectedAuthorId = -1,
                SelectedBookId   = -1
            };

            db.Remove(db.Query <Request>().FirstOrDefault(r => r.RequestId == request.RequestId));
            db.SaveChanges();
            return(View(model));
        }
        public ActionResult Edit(AddAuthorToBook model, int page, string orderBy, string search, string bookGenre)
        {
            if (ModelState.IsValid)
            {
                Book book = db.Query <Book>().FirstOrDefault(b => b.BookId == model.Book.BookId);
                if (book == null)
                {
                    return(HttpNotFound());
                }

                book.Name        = model.Book.Name;
                book.Description = model.Book.Description;
                book.Genre       = model.Book.Genre;
                book.ImageUrl    = model.Book.ImageUrl;
                book.Rating      = model.Book.Rating;
                book.Year        = model.Book.Year;
                book.InStock     = model.Book.InStock;

                Author author = db.Query <Author>().FirstOrDefault(a => a.AuthorId == model.SelectedAuthorId);
                if (author == null)
                {
                    model = new AddAuthorToBook()
                    {
                        Book             = new Book(),
                        Authors          = db.Query <Author>().ToList(),
                        SelectedAuthorId = -1,
                        SelectedBookId   = -1
                    };
                    return(View(model));
                }
                book.Author = author;

                db.SaveChanges();
                return(RedirectToAction("Index", new { page, orderBy, search, bookGenre }));
            }
            return(View(model));
        }
        public ActionResult Create(AddAuthorToBook model)
        {
            if (ModelState.IsValid)
            {
                Book book = new Book()
                {
                    Name        = model.Book.Name,
                    Genre       = model.Book.Genre,
                    Year        = model.Book.Year,
                    Rating      = model.Book.Rating,
                    Description = model.Book.Description,
                    ImageUrl    = model.Book.ImageUrl
                };

                Author author = db.Query <Author>().FirstOrDefault(a => a.AuthorId == model.SelectedAuthorId);
                if (author == null)
                {
                    model = new AddAuthorToBook()
                    {
                        Book             = new Book(),
                        Authors          = db.Query <Author>().ToList(),
                        SelectedAuthorId = -1,
                        SelectedBookId   = -1
                    };

                    return(View(model));
                }
                book.Author = author;

                db.Add(book);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(model));
        }