Main.Controllers.CategoriesController.DeleteConfirmed C# (CSharp) Method

DeleteConfirmed() private method

private DeleteConfirmed ( int id ) : System.Web.Mvc.ActionResult
id int
return System.Web.Mvc.ActionResult
        public ActionResult DeleteConfirmed(int id)
        {
            Category category = db.Categories.Find(id);

            // don't allow the category to be deleted if its being used
            int postsWithCategory = db.Posts.Where(p => p.CategoryID == category.CategoryID).Count();
            if (postsWithCategory > 0) {
                return RedirectToAction("Delete");
            }

            // remove the category
            db.Categories.Remove(category);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

Usage Example

 public void testDeleteConfirmed()
 {
     CategoriesController categoriesController = new CategoriesController();
     ActionResult deleteConfirmed = categoriesController.DeleteConfirmed(5);
 }