public JsonResult CreateBook([DataSourceRequest] DataSourceRequest request, DetailedBookViewModel book)
        {
            

            if (book != null && ModelState.IsValid)
            {
                var category = this.db.Categories.FirstOrDefault(x => x.Id == int.Parse(book.CategoryName));

                
                var newBook = new Book
                {
                    Title = book.Title,
                    Description = book.Description,
                    Author = book.Author,
                    Category=category
                  
                };

                this.db.Books.Add(newBook);
                this.db.SaveChanges();

                book.Id = newBook.Id;
            }

            return Json(new[] { book }.ToDataSourceResult(request, ModelState), JsonRequestBehavior.AllowGet);
        }
Exemplo n.º 2
0
        public JsonResult DeleteBook([DataSourceRequest] DataSourceRequest request, DetailedBookViewModel book)
        {
            var existingBook = this.Data.Books.FirstOrDefault(x => x.Id == book.Id);

            this.Data.Books.Remove(existingBook);
            this.Data.SaveChanges();

            return Json(new[] { book }, JsonRequestBehavior.AllowGet);
        }
        public JsonResult UpdateBook([DataSourceRequest] DataSourceRequest request, DetailedBookViewModel book)
        {
            var existingBook = this.db.Books.FirstOrDefault(x => x.Id == book.Id);

            if (book != null && ModelState.IsValid)
            {
                existingBook.Title = book.Title;
                existingBook.Description = book.Description;
                existingBook.Author = book.Author;
                existingBook.Category = this.db.Categories.FirstOrDefault(x => x.Name == book.CategoryName);

                this.db.SaveChanges();
            }

            return Json((new[] { book }.ToDataSourceResult(request, ModelState)), JsonRequestBehavior.AllowGet);
        }