Example #1
0
        public ActionResult Destroy([DataSourceRequest] DataSourceRequest request, CategoryGridModel category)
        {
            if (category != null)
            {
                var context = new ApplicationDbContext();
                var categoryToDelete = context.Categories.FirstOrDefault(c => c.Id == category.Id);
                context.Books.RemoveRange(categoryToDelete.Books);
                context.SaveChanges();
                context.Categories.Remove(categoryToDelete);
                context.SaveChanges();
            }

            return Json(new[] { category }.ToDataSourceResult(request, ModelState));
        }
Example #2
0
        public ActionResult Create([DataSourceRequest] DataSourceRequest request, CategoryGridModel category)
        {
            if ((category.Name == null) || (category.Name.Length < 2))
            {
                ModelState.AddModelError("Name", "Name should be at least 2 characters long.");
                return Json(new[] { category }.ToDataSourceResult(request, ModelState));
            }

            if (category != null && ModelState.IsValid)
            {
                var context = new ApplicationDbContext();
                context.Categories.Add(new Category() { Name = category.Name });
                context.SaveChanges();
            }

            return Json(new[] { category }.ToDataSourceResult(request, ModelState));
        }
Example #3
0
        public ActionResult Update([DataSourceRequest] DataSourceRequest request, CategoryGridModel category)
        {
            if ((category.Name == null) || (category.Name.Length < 2))
            {
                ModelState.AddModelError("Name", "Name should be at least 2 characters long.");
                return Json(new[] { category }.ToDataSourceResult(request, ModelState));
            }

            if (category != null && ModelState.IsValid)
            {
                var context = new ApplicationDbContext();
                var target = context.Categories.FirstOrDefault(p => p.Id == category.Id);
                if (target != null)
                {
                    target.Name = category.Name;
                    context.SaveChanges();
                }
            }

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