public ActionResult CreateCategory([DataSourceRequest] DataSourceRequest request, CategoryViewModel category)
 {
     if (category != null && ModelState.IsValid)
     {
         var newCategory = new Category()
         {
             CategoryName = category.viewCategoryName,
             CategoryValidFrom = category.viewCategoryValidFrom
         };
         db.Categories.Add(newCategory);
         db.SaveChanges();
         category.viewCategoryId = newCategory.CategoryId;
     }
     return Json(new[] { category }.ToDataSourceResult(request, ModelState));
 }
        public ActionResult DestroyCategory([DataSourceRequest] DataSourceRequest request, CategoryViewModel category)
        {
            if (category != null)
            {
                var newCategory = new Category() { CategoryId = category.viewCategoryId };
                db.Categories.Attach(newCategory);
                db.Categories.Remove(newCategory);

                var products = db.Products.Where(x => x.ProductCategoryId == category.viewCategoryId);
                foreach (var product in products)
                {
                    db.Products.Attach(product);
                    db.Products.Remove(product);
                }
                db.SaveChanges();
            }
            return Json(new[] { category }.ToDataSourceResult(request, ModelState));
        }
 public ActionResult UpdateCategory([DataSourceRequest] DataSourceRequest request, CategoryViewModel category)
 {
     if (category != null && ModelState.IsValid)
     {
         var newCategory = new Category()
         {
             CategoryId = category.viewCategoryId,
             CategoryName = category.viewCategoryName,
             CategoryValidFrom = category.viewCategoryValidFrom
         };
         db.Categories.Attach(newCategory);
         db.Entry(newCategory).State = EntityState.Modified;
         db.SaveChanges();
     }
     return Json(new[] { category }.ToDataSourceResult(request, ModelState));
 }