Section 3 of 5
Handlers (OnGet, OnPost)
🎯 What You'll Learn
- Handler methods
- OnGet and OnPost
- Named handlers
- Handler parameters
- Async handlers
What are Handlers?
Handlers are methods in PageModel that respond to HTTP requests (GET, POST, etc.).
OnGet Handler
Handles HTTP GET requests (displaying pages).
OnGet Handler
C#
public class IndexModel : PageModel
{
public List<Product> Products { get; set; } = new();
public async Task OnGetAsync()
{
Products = await _context.Products.ToListAsync();
}
}
OnPost Handler
Handles HTTP POST requests (form submissions).
OnPost Handler
C#
public class CreateModel : PageModel
{
[BindProperty]
public Product Product { get; set; } = new();
public IActionResult OnGet()
{
return Page();
}
public async Task<IActionResult> OnPostAsync()
{
if (!ModelState.IsValid)
return Page();
_context.Products.Add(Product);
await _context.SaveChangesAsync();
return RedirectToPage("Index");
}
}
Named Handlers
Handle multiple actions on the same page.
Named Handlers
C#
public class ProductModel : PageModel
{
public IActionResult OnPostDelete(int id)
{
// Delete product
return RedirectToPage();
}
public IActionResult OnPostApprove(int id)
{
// Approve product
return RedirectToPage();
}
}
Call Named Handler in View
HTML
<form method="post" asp-page-handler="Delete">
<button type="submit">Delete</button>
</form>
<form method="post" asp-page-handler="Approve">
<button type="submit">Approve</button>
</form>
Handler Parameters
Route and Query Parameters
C#
// Route parameter: @page "{id:int}"
public async Task OnGetAsync(int id)
{
Product = await _context.Products.FindAsync(id);
}
// Query parameter: ?search=laptop
public async Task OnGetAsync(string? search)
{
var query = _context.Products.AsQueryable();
if (!string.IsNullOrEmpty(search))
query = query.Where(p => p.Name.Contains(search));
Products = await query.ToListAsync();
}
Complete InvenTrack Example
Pages/Products/Edit.cshtml.cs
C#
public class EditModel : PageModel
{
private readonly InvenTrackDbContext _context;
public EditModel(InvenTrackDbContext context) => _context = context;
[BindProperty]
public Product Product { get; set; } = new();
public async Task<IActionResult> OnGetAsync(int id)
{
Product = await _context.Products.FindAsync(id);
if (Product == null)
return NotFound();
return Page();
}
public async Task<IActionResult> OnPostAsync()
{
if (!ModelState.IsValid)
return Page();
_context.Update(Product);
await _context.SaveChangesAsync();
TempData["Success"] = "Product updated!";
return RedirectToPage("Index");
}
public async Task<IActionResult> OnPostDeleteAsync(int id)
{
var product = await _context.Products.FindAsync(id);
if (product != null)
{
_context.Products.Remove(product);
await _context.SaveChangesAsync();
TempData["Success"] = "Product deleted!";
}
return RedirectToPage("Index");
}
}
Key Takeaways
- OnGet: Handles GET requests
- OnPost: Handles POST requests
- Named handlers: OnPost{Name} for multiple actions
- asp-page-handler: Specify handler in view
- Parameters: Route and query parameters
- Async: Use OnGetAsync/OnPostAsync