Overview
The Customer Management module provides complete CRUD functionality for managing customer information. The system stores detailed customer profiles including personal information, contact details, and addresses.
Customer Model
The Customer model (Models/Customer.cs:3-13) defines a comprehensive customer profile:
public class Customer
{
public int Id { get ; set ; }
public string Document { get ; set ; }
public string FirstName { get ; set ; }
public string LastName { get ; set ; }
public string Address { get ; set ; }
public DateTime Birthday { get ; set ; }
public string PhoneNumber { get ; set ; }
public string Email { get ; set ; }
}
Properties
Property Type Description Example Idint Unique identifier 1 Documentstring Government-issued ID number ”12345678” FirstNamestring Customer’s first name ”John” LastNamestring Customer’s last name ”Smith” Addressstring Physical address ”123 Main St” BirthdayDateTime Date of birth 1990-05-15 PhoneNumberstring Contact phone number ”+1-555-0123” Emailstring Email address ”[email protected] ”
The Customer model captures all essential information needed for customer relationship management, loyalty programs, and communication.
CRUD Operations
List Customers
Create Customer
Edit Customer
Delete Customer
Viewing All Customers The Index page (Pages/Customers/Index.cshtml.cs:22-28) displays all customer records: public IList < Customer > Customers { get ; set ; } = default ! ;
public async Task OnGetAsync ()
{
if ( _context . Customers != null )
{
Customers = await _context . Customers . ToListAsync ();
}
}
The page loads all customers asynchronously and exposes them through the Customers property for display in the view. Adding New Customers New customers are added through the Create page following the standard pattern: [ BindProperty ]
public Customer Customer { get ; set ; } = default ! ;
public async Task < IActionResult > OnPostAsync ()
{
if ( ! ModelState . IsValid || _context . Customers == null || Customer == null )
{
return Page ();
}
_context . Customers . Add ( Customer );
await _context . SaveChangesAsync ();
return RedirectToPage ( "./Index" );
}
Input Validation
ModelState checks ensure all required fields are provided
Add to Context
Customer is added to Entity Framework context
Persist Data
SaveChangesAsync commits the new customer to the database
Navigate
User is redirected to the customer list
The Edit page handles customer updates: [ BindProperty ]
public Customer Customer { get ; set ; } = default ! ;
public async Task < IActionResult > OnGetAsync ( int ? id )
{
if ( id == null || _context . Customers == null )
{
return NotFound ();
}
var customer = await _context . Customers . FirstOrDefaultAsync ( m => m . Id == id );
if ( customer == null )
{
return NotFound ();
}
Customer = customer ;
return Page ();
}
public async Task < IActionResult > OnPostAsync ()
{
if ( ! ModelState . IsValid )
{
return Page ();
}
_context . Attach ( Customer ). State = EntityState . Modified ;
try
{
await _context . SaveChangesAsync ();
}
catch ( DbUpdateConcurrencyException )
{
if ( ! CustomerExists ( Customer . Id ))
{
return NotFound ();
}
else
{
throw ;
}
}
return RedirectToPage ( "./Index" );
}
Removing Customer Records The Delete page provides confirmation before removing customers: [ BindProperty ]
public Customer Customer { get ; set ; } = default ! ;
public async Task < IActionResult > OnGetAsync ( int ? id )
{
if ( id == null || _context . Customers == null )
{
return NotFound ();
}
var customer = await _context . Customers . FirstOrDefaultAsync ( m => m . Id == id );
if ( customer == null )
{
return NotFound ();
}
else
{
Customer = customer ;
}
return Page ();
}
public async Task < IActionResult > OnPostAsync ( int ? id )
{
if ( id == null || _context . Customers == null )
{
return NotFound ();
}
var customer = await _context . Customers . FindAsync ( id );
if ( customer != null )
{
Customer = customer ;
_context . Customers . Remove ( customer );
await _context . SaveChangesAsync ();
}
return RedirectToPage ( "./Index" );
}
Document Government-issued identification number for unique customer identification
Name Full name split into FirstName and LastName for proper addressing
Birthday Date of birth for age verification and birthday promotions
Address Physical address for delivery and communication purposes
Email Primary email address for digital communication
Phone Number Contact phone number for urgent communication
Authorization
All customer management operations require authentication:
[ Authorize ]
public class IndexModel : PageModel
{
// Implementation
}
Customer data contains personally identifiable information (PII). Only authenticated users can access customer management features.
Database Context
Customers are managed through the SupermarketContext (Data/SupermarketContext.cs:15):
public DbSet < Customer > Customers { get ; set ; }
Page Structure
The customer management module consists of four Razor Pages:
Page Route Purpose Index /Pages/Customers/Index.cshtmlDisplay all customers Create /Pages/Customers/Create.cshtmlAdd new customer Edit /Pages/Customers/Edit.cshtmlUpdate customer details Delete /Pages/Customers/Delete.cshtmlRemove customer record
Validation Best Practices
Consider adding validation attributes to ensure data quality:
public class Customer
{
public int Id { get ; set ; }
[ Required ]
[ StringLength ( 20 )]
public string Document { get ; set ; }
[ Required ]
[ StringLength ( 50 )]
public string FirstName { get ; set ; }
[ Required ]
[ StringLength ( 50 )]
public string LastName { get ; set ; }
[ Required ]
[ StringLength ( 200 )]
public string Address { get ; set ; }
[ Required ]
[ DataType ( DataType . Date )]
public DateTime Birthday { get ; set ; }
[ Required ]
[ Phone ]
[ StringLength ( 20 )]
public string PhoneNumber { get ; set ; }
[ Required ]
[ EmailAddress ]
[ StringLength ( 100 )]
public string Email { get ; set ; }
}
Adding data annotations improves input validation and provides better user feedback when invalid data is entered.
Common Operations
Search by Email
var customer = await _context . Customers
. FirstOrDefaultAsync ( c => c . Email == email );
Search by Document
var customer = await _context . Customers
. FirstOrDefaultAsync ( c => c . Document == documentNumber );
Find Customers by Name
var customers = await _context . Customers
. Where ( c => c . FirstName . Contains ( searchTerm ) || c . LastName . Contains ( searchTerm ))
. ToListAsync ();
Birthday List for Current Month
var currentMonth = DateTime . Now . Month ;
var birthdayCustomers = await _context . Customers
. Where ( c => c . Birthday . Month == currentMonth )
. OrderBy ( c => c . Birthday . Day )
. ToListAsync ();
Privacy and Security
PII Protection Customer data contains sensitive PII requiring proper protection
Access Control Implement role-based access for viewing customer information
Audit Logging Track who accesses and modifies customer records
Data Encryption Consider encrypting sensitive fields at rest and in transit
Advanced Features
Group customers by demographics, purchase history, or behavior for targeted marketing campaigns.
Use customer data to implement point-based loyalty programs and track reward balances.
Link customers to their transaction history for personalized recommendations and analytics.
Communication Preferences
Add fields to track customer preferences for email, SMS, and promotional communications.
GDPR Compliance
For international deployments, consider implementing:
Right to Access : Allow customers to request their data
Right to Rectification : Easy data correction mechanisms
Right to Erasure : “Delete my account” functionality
Data Portability : Export customer data in standard formats
Consent Management : Track consent for data processing and marketing
Deleting customer records may impact referential integrity if linked to orders or transactions. Consider soft deletes or data archival strategies.