コード例 #1
0
        public ActionResult CategoriesDestroy([DataSourceRequest]DataSourceRequest request, CategoryViewModel category)
        {
            if (ModelState.IsValid)
            {
                var currCategory = db.Categories.Find(category.Id);
                db.Categories.Remove(currCategory);
                db.SaveChanges();
            }

            return Json(new[] { category }.ToDataSourceResult(request, ModelState));
        }
コード例 #2
0
        public ActionResult Delete([DataSourceRequest] DataSourceRequest request, CategoryViewModel category)
        {
            if (category != null)
            {
                var categoryDb = db.Categories.Find(category.Id);
                db.Books.RemoveRange(categoryDb.Books);
                db.Categories.Remove(categoryDb);
                db.SaveChanges();
            }

            return Json(new[] { category }.ToDataSourceResult(request), JsonRequestBehavior.AllowGet);
        }
コード例 #3
0
        public ActionResult CategoriesCreate([DataSourceRequest]DataSourceRequest request, CategoryViewModel Category)
        {
            if (ModelState.IsValid)
            {
                var entity = new Category
                {
                    Name = Category.Name,
                };

                db.Categories.Add(entity);
                db.SaveChanges();
                Category.Name = entity.Name;
            }

            return Json(new[] { Category }.ToDataSourceResult(request, ModelState), JsonRequestBehavior.AllowGet);
        }
コード例 #4
0
        public ActionResult Update([DataSourceRequest] DataSourceRequest request, CategoryViewModel category)
        {
            if (category != null && ModelState.IsValid)
            {
                if (string.IsNullOrWhiteSpace(category.Name))
                {
                    ModelState.AddModelError("CategoryName", "The category name is required.");
                }

                var categoryDb = db.Categories.Find(category.Id);
                categoryDb.Name = category.Name;
                db.SaveChanges();
            }

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