liveBook · Manning

PageModel

As explained in ASP.NET Core in Action, 3e

A PageModel code-behind file is the C# source file containing the handler methods and state for a Razor Page.

A PageModel is the backing class for a Razor Page. It handles requests and exposes data for the Razor view to render. It usually lives in a C# code-behind file alongside the view: for example, Privacy.cshtml.cs accompanies Privacy.cshtml. section 13.1.4 section 13.4.3

The matching filenames are a convention, not the link between the files. The Razor view starts with @page and normally uses an @model directive to identify its PageModel type. A separate PageModel is optional; without one, the view can render through an implicit handler. section 17.2.1

A typical PageModel works with a request in a few steps:

  • A handler such as OnGet runs for a GET request. A void handler renders the associated view; an IActionResult can instead direct another response, such as a redirect. section 13.1.4
  • Model binding supplies request data to handler parameters or properties marked with [BindProperty]. Validation runs after binding, but the handler must check ModelState.IsValid and decide what to do. section 16.5
  • The handler generally delegates business logic to services, then sets PageModel properties needed by the view. The view uses those properties to generate HTML; it should generally not call PageModel methods. section 13.5 section 17.1

A category page

For a request to /category/Simple, the category value is bound to the OnGet parameter. The handler asks an injected ToDoService for matching items, assigns them to the Items property, and returns Page() so the view can render them. section 13.4.1 section 13.2

Listing 13.5 A Razor Page for viewing all to-do items in a given category
public class CategoryModel : PageModel
{
    private readonly ToDoService _service;                #1
    public CategoryModel(ToDoService service)             #1
    {
        _service = service;
    }
 
    public ActionResult OnGet(string category)            #2
    {
        Items = _service.GetItemsForCategory(category);   #3
        return Page();                                    #4
    }
 
    public List<ToDoListModel> Items { get; set; }        #5
}

See the complete CategoryModel handler, constructor injection, and Items property together. section 13.2

A PageModel has two roles: its handlers act as the controller, while its properties act as the view model. It is distinct from the binding model that carries request data and the application model that performs business actions. section 16.1

How ASP.NET Core in Action, 3e covers it

How deep it goes
Explains handlers, binding, service delegation, and view data through code examples, while deferring detailed binding and routing.
Examples
Uses a privacy page and a category-filtered to-do list to illustrate handlers, injected services, and view properties.
What you can do after
Apply PageModel in ASP.NET Core Razor Pages to handle requests, use services, and supply data to a view.
How it connects
Connects PageModel to MVC, model binding, dependency injection, Razor views, and validation.

Also explained in

Compare how 2 books explain PageModel →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of ASP.NET Core in Action, 3e. The section links point to the parts of the book it draws on.