public ActionResult Create([DataSourceRequest] DataSourceRequest request, BooksViewModel book) { if (book != null && ModelState.IsValid) { var category = db.Categories.FirstOrDefault(c => c.Id == book.Category.Id); var bookToCreate = new Book(); bookToCreate.Id = book.Id; bookToCreate.Author = book.Author; bookToCreate.Category = category; bookToCreate.Description = book.Description; bookToCreate.ISBN = book.ISBN; bookToCreate.Title = book.Title; bookToCreate.WebSite = book.WebSite; db.Books.Add(bookToCreate); db.SaveChanges(); } return Json(ModelState.IsValid ? true : ModelState.ToDataSourceResult()); }
public ActionResult Edit([DataSourceRequest] DataSourceRequest request, BooksViewModel book) { if (book != null && ModelState.IsValid) { var bookToUpdate = db.Books.FirstOrDefault(b => b.Id == book.Id); bookToUpdate.Author = book.Author; bookToUpdate.Category.Id = book.Category.Id; bookToUpdate.Category.Name = book.Category.Name; bookToUpdate.Description = book.Description; bookToUpdate.ISBN = book.ISBN; bookToUpdate.Title = book.Title; bookToUpdate.WebSite = book.WebSite; db.SaveChanges(); } return Json(ModelState.IsValid ? true : ModelState.ToDataSourceResult()); }
public ActionResult Delete([DataSourceRequest] DataSourceRequest request, BooksViewModel book) { if (book != null && ModelState.IsValid) { var bookToDelete = db.Books.Include(b => b.Category).FirstOrDefault(b => b.Id == book.Id); db.Books.Remove(bookToDelete); db.SaveChanges(); } return Json(ModelState.IsValid ? true : ModelState.ToDataSourceResult()); }