Overview
The Category Management module provides CRUD operations for organizing products into categories. Categories help structure the product catalog and enable filtering and navigation.Category Model
The Category model (Models/Category.cs:5-11) defines the structure for product categories:
Properties
| Property | Type | Required | Description |
|---|---|---|---|
Id | int | Yes | Unique identifier |
Name | string | Yes | Category name |
Description | string? | No | Optional category description |
Products | ICollection<Product>? | No | Collection of products in this category |
The
Products collection is a navigation property that establishes a one-to-many relationship with the Product model.CRUD Operations
Listing Categories
The Index page (Pages/Categories/Index.cshtml.cs:22-28) retrieves all categories:
Categories property that contains all categories from the database. Entity Framework Core’s ToListAsync() efficiently loads the data asynchronously.
Creating Categories
Categories can be created through the/Pages/Categories/Create.cshtml page, following the standard pattern:
- GET Handler
- POST Handler
Product Relationship
Categories maintain a one-to-many relationship with products:Relationship Benefits
Product Organization
Group related products together for easier management
Navigation
Enable filtering and browsing products by category
Data Integrity
Foreign key relationship ensures valid product-category associations
Cascade Operations
Handle dependent product records when modifying categories
Authorization
All category management operations require authentication:Database Context
Categories are managed through theSupermarketContext (Data/SupermarketContext.cs:14):
Page Structure
The category management module follows the standard CRUD structure:| Page | Route | Purpose |
|---|---|---|
| Index | /Pages/Categories/Index.cshtml | List all categories |
| Create | /Pages/Categories/Create.cshtml | Add new category |
| Edit | /Pages/Categories/Edit.cshtml | Modify existing category |
| Delete | /Pages/Categories/Delete.cshtml | Remove category |
Implementation Pattern
All category pages follow a consistent pattern:Dependency Injection
Dependency Injection
Model Binding
Model Binding
[BindProperty] attribute enables automatic form data binding.Async Operations
Async Operations
Loading Related Products
To load categories with their products, use Entity Framework’s Include method:The
Include method should only be used when you actually need the related products. For the category list page, loading products for every category may be unnecessary and impact performance.Validation
Consider adding validation attributes to ensure data quality:Best Practices
Unique Names
Ensure category names are unique to avoid confusion
Soft Delete
Consider soft deletes instead of hard deletes to preserve history
Hierarchical Categories
For complex catalogs, implement parent-child category relationships
Category Counts
Display product counts per category for better insights