skip to content

In ASP.NET Core Razor Pages, what does [BindProperty] do on a PageModel property, and why does a GET request need SupportsGet = true?

level: middleimportance: must knowfreq 52%

answer

  1. bound before the handler runs
  2. non-GET requests only by default
  3. SupportsGet = true opts in
  4. [BindProperties] for the whole class
  5. the page and asp-for can read it

basics

~20 s

[BindProperty] makes model binding fill a PageModel property before the handler runs, so the handler and the page both use it. Only non-GET requests are bound by default; SupportsGet = true also binds GET, such as a search term.

solid answer

~50 s

Model binding normally fills handler parameters; `[BindProperty]` makes it fill a **PageModel property** too, after the handler is selected and before it runs. The bound value lives on the model, so the handler uses it and the page renders it through `Model.Input` and `asp-for`, which is why Razor Pages forms bind to an `Input` property. The attribute implements a request predicate: with the default `SupportsGet = false` it binds only non-GET requests, because a GET usually needs just an id and should not overwrite the property from the query string. `[BindProperty(SupportsGet = true)]` opts in, for example a `Search` term in `?search=molar`. `[BindProperties]` on the class binds every public property and has its own `SupportsGet`, and `Name` changes the request key. Bound properties are filled for **every** handler of a matching verb, named handlers included.

code

csharp · 14 lines
csharp
public class IndexModel(IAppointmentService appointments) : PageModel
{
    // Filled on GET from ?search=... because SupportsGet is true
    [BindProperty(SupportsGet = true)]
    public string? Search { get; set; }

    // Display-only: loaded by the handler, never bound
    public IReadOnlyList<AppointmentRow> Rows { get; private set; } = [];

    public async Task OnGetAsync()
    {
        Rows = await appointments.SearchAsync(Search);
    }
}

go deeper

for a junior

Recall that [BindProperty] fills a PageModel property from the request and that GET requests are skipped unless SupportsGet = true.

for a middle

Explain when binding runs in the page pipeline, why forms bind an Input property, and the trade-off against handler parameters.

for a senior

Prevent over-posting with input models, scope bound properties so named handlers do not fail validation, and keep display state unbound.

for a principal

Set page-model conventions for a team, such as input models only and no [BindProperties] on mixed classes, so binding stays reviewable.

## Two places binding can put a value ASP.NET Core model binding reads route values, the query string and form fields and turns them into .NET values. In a controller action those values arrive as **parameters**. A Razor Pages handler can take parameters too (`OnGetAsync(int id)`), but pages add a second target: **properties** of the `PageModel` marked `[BindProperty]`. The difference matters because the page's view renders the model. A dentist's booking page binds the posted form to an `Input` property, and when validation fails the same property feeds the re-rendered form: ```csharp public class BookModel(IAppointmentService appointments) : PageModel { [BindProperty] public BookingInput Input { get; set; } = new(); public async Task<IActionResult> OnPostAsync() { if (!ModelState.IsValid) return Page(); // form shows Input again await appointments.BookAsync(Input); return RedirectToPage("./Index"); } } ``` In the `.cshtml`, `<input asp-for="Input.PatientName" />` generates `name="Input.PatientName"`, which is exactly the key the binder reads back into `Input.PatientName` on the POST. ## When binding happens For a page request the pipeline runs in this order: 1. The handler is selected from the verb and handler name. 2. Page filters' `OnPageHandlerSelected` runs. 3. **Bound properties and handler parameters are bound**, and validation results land in `ModelState`. 4. `OnPageHandlerExecuting` runs, then the handler, then `OnPageHandlerExecuted`. So inside the handler, `Input` is already filled. A bound property is filled for **every** handler whose verb matches, so a page with `OnPostAsync` and a named `OnPostCancelAsync` binds and validates `Input` for both. Values that only one handler needs fit better as that handler's parameters. ## Why GET is excluded by default `BindPropertyAttribute` carries a request predicate. With `SupportsGet = false`, the default, it binds only when the request method is not GET: | Declaration | GET | POST, PUT, DELETE | |---|---|---| | `[BindProperty]` | not bound | bound | | `[BindProperty(SupportsGet = true)]` | bound | bound | | `[BindProperties]` on the class | not bound | bound (every public property) | | `[BindProperties(SupportsGet = true)]` | bound | bound | | handler parameter `OnGetAsync(int id)` | bound | bound | The reasoning in the docs: a GET usually needs only a record id to look something up, so there is no need to bind a whole model property on GET. When a GET does carry input you want on the model, such as a search box or a filter that the page re-renders, set `SupportsGet = true`: ```csharp [BindProperty(SupportsGet = true)] public string? Search { get; set; } // /Appointments?search=molar ``` ## Other knobs - `[BindProperty(Name = "q")]` binds from the key `q` instead of the property name. - `BinderType` plugs in a custom `IModelBinder` for one property. - `[BindProperty]` is not limited to pages: it also works on controller properties, with the same GET rule. ## Explicit binding as the alternative `[BindProperty]` binds automatically on every matching request. When a handler needs control over **what** is bound and **when**, `PageModel` also offers explicit binding: ```csharp public async Task<IActionResult> OnPostAsync(int id) { var appointment = await db.Appointments.FindAsync(id); if (appointment is null) return NotFound(); // bind only these two fields from the "Input" prefix if (await TryUpdateModelAsync(appointment, "Input", a => a.Notes, a => a.StartsAt)) { await db.SaveChangesAsync(); return RedirectToPage("./Details", new { id }); } return Page(); } ``` `TryUpdateModelAsync` runs binding and validation on an object you already loaded, limited to the listed properties, and returns whether `ModelState` is valid. It suits edit pages where the record must be loaded first; `[BindProperty]` with a separate input model suits create forms. ## Mistakes interviewers look for - **Binding an entity directly.** `[BindProperty] public Appointment Appointment` lets a crafted form set any property the entity has, not just the fields you rendered. Bind a dedicated input model and copy the allowed fields. - **Expecting a GET to fill a property.** A `[BindProperty]` search term stays `null` on `/Appointments?search=molar` until `SupportsGet = true` is set. - **Forgetting the property is bound for every POST handler.** Validation on `Input` then fails for a Cancel button that never sends those fields. - **Using `[BindProperty]` for data the page only displays.** Values loaded in `OnGetAsync` need no attribute, and marking them invites clients to overwrite them on POST.

  • When would you bind a handler parameter instead of a [BindProperty] property?
    When the value matters to one handler and the page does not need to render it: an `id` in `OnGetAsync(int id)` or a reason in `OnPostCancelAsync(string reason)`. Parameters are bound on GET without any opt-in and are bound only for the handler that declares them. Use a bound property when the page re-renders the value, typically the form's `Input` model after a failed POST.
  • What does [BindProperties] on the PageModel class change?
    It marks every public property of the class as bound, as if each had `[BindProperty]`, with its own `SupportsGet` that defaults to `false`. It saves attributes on input-only page models, but it also binds any public property you add later, so keep display-only state out of such a class.

  • [BindProperty] properties are bound on GET requests by default
  • Binding the EF Core entity with [BindProperty] is the recommended pattern
  • [BindProperty] values are bound only for the unnamed OnPost handler
  • Every public PageModel property is bound even without [BindProperty]
  • Handler parameters need SupportsGet = true to bind on GET