CategoryController Class in ASP.NET Core MVC
The CategoryController class in ASP.NET Core MVC is responsible for managing categories in a web application. This article will provide a detailed explanation of the class, its properties, and methods.
Overview of the Controller
The CategoryController class allows for the following operations:
- Displaying a list of categories
- Creating new categories
- Updating existing categories
- Deleting categories
The CategoryController Class
[Area("Manage")]
public class CategoryController : Controller
{
private readonly AppDbContext _context;
public CategoryController(AppDbContext context)
{
_context = context;
}
// GET: Category/Index
public async Task<IActionResult> Index()
{
var categories = await _context.Categories.Include(p => p.Products).ToListAsync();
return View(categories);
}
// GET: Category/Create
public IActionResult Create()
{
return View();
}
// POST: Category/Create
[HttpPost]
public IActionResult Create(Category category)
{
if (!ModelState.IsValid)
{
return View(category);
}
_context.Categories.Add(category);
_context.SaveChanges();
return RedirectToAction(nameof(Index));
}
// GET: Category/Delete/5
public IActionResult Delete(int? Id)
{
if (Id == null)
{
return NotFound();
}
var category = _context.Categories.FirstOrDefault(p => p.Id == Id);
if (category == null)
{
return NotFound();
}
_context.Categories.Remove(category);
_context.SaveChanges();
return RedirectToAction(nameof(Index));
}
// GET: Category/Update/5
public IActionResult Update(int? Id)
{
if (Id == null)
{
return NotFound();
}
var category = _context.Categories.FirstOrDefault(p => p.Id == Id);
if (category == null)
{
return NotFound();
}
return View(category);
}
// POST: Category/Update/5
[HttpPost]
public IActionResult Update(Category newCategory)
{
if (!ModelState.IsValid)
{
return View(newCategory);
}
var oldCategory = _context.Categories.FirstOrDefault(p => p.Id == newCategory.Id);
if (oldCategory == null)
{
return NotFound();
}
oldCategory.Name = newCategory.Name;
_context.SaveChanges();
return RedirectToAction(nameof(Index));
}
}
Breaking Down the Actions
- Index: This action is used to display a list of categories. It uses Entity Framework Core’s Include method to load related data, and returns the list of categories to the view.
- Create: This action is used to create a new category. It displays an empty form view.
- Create: This action is used to handle the form submission for creating a new category. It validates the model, adds the new category to the database, and returns a redirect to the index action.
- Delete: This action is used to delete a category. It checks if the category exists, removes it from the database, and returns a redirect to the index action.
- Update: This action is used to update an existing category. It displays the form view with the existing category data.
- Update: This action is used to handle the form submission for updating an existing category. It validates the model, updates the category in the database, and returns a redirect to the index action.
Conclusion
In this article, we have learned how to create a CategoryController class in ASP.NET Core MVC, which handles the most common CRUD operations for categories. This setup can be easily extended to include more complex features, such as category hierarchies, custom sorting, and pagination, depending on the requirements of your application.
FAQs
- Q: What is the purpose of the CategoryController class?
A: The CategoryController class is responsible for managing categories in a web application. - Q: What operations can the CategoryController class perform?
A: The CategoryController class can perform the following operations: display a list of categories, create new categories, update existing categories, and delete categories. - Q: How does the CategoryController class handle validation?
A: The CategoryController class uses the ModelState class to validate the model. If the model is invalid, it returns the view with validation errors. - Q: How does the CategoryController class interact with the database?
A: The CategoryController class uses Entity Framework Core to interact with the database. It uses the Include method to load related data and the SaveChanges method to save changes to the database.

