liveBook · Manning

BindProperty attribute

As explained in Pro ASP.NET Core 7, Tenth Edition

The BindProperty attribute is an ASP.NET Core attribute that marks a page-model property for binding from request data.

In a Razor Page, `[BindProperty]` tells the model binder to populate a property from request data. It lets a handler use an existing page-model property instead of declaring a duplicate parameter. For a complex property such as `Product`, its member values can be bound from form fields. section 28.4.1

The details that usually matter are:

  • The binder looks for values using the property name. An input using `asp-for="Product.Name"` can supply a value for a bound `Product` property without a separately specified input name. section 28.4.1
  • GET requests are not bound by default. Set `SupportsGet = true` when the property must receive a value from a GET request. section 28.4.1 section 9.3.7
  • Use the `Name` argument when the request-data prefix differs from the property name, including when binding a nested complex value. section 28.4.2
  • Use `[BindProperties]` on a class to bind all its public properties, and `[BindNever]` on properties that should be excluded. section 28.4.1
Listing 9.22 The contents of the Completed.cshtml file in the SportsStore/Pages folder
@page

<div class="text-center">
   <h2>Thanks!</h2>
   <p>Thanks for placing order #@OrderId</p>
   <p>We'll ship your goods as soon as possible.</p>
   <a class="btn btn-primary" asp-controller="Home">Return to Store</a>
</div>

@functions {

   [BindProperty(SupportsGet = true)]
   public string? OrderId { get; set; }
}

The completed-order Razor Page puts `[BindProperty(SupportsGet = true)]` on `OrderId` and displays that value in its thank-you message. The small page needs no separate page-model class. section 9.3.7

Binding several form values

A `/pages/bindings` Razor Page gives three inputs the name `Data` and decorates its `Data` array property with `[BindProperty]`, using `Name` to identify the values. Submitting the form populates an array that the page displays. Names such as `Data[1]` and `Data[0]` can specify positions instead; empty fields can produce null values. section 28.5.1

How Pro ASP.NET Core 7, Tenth Edition covers it

How deep it goes
Explains name matching, GET opt-in, prefixes, complex properties, and arrays through form examples; does not detail binder internals.
Examples
Uses a completed-order identifier, a product form, and repeated or indexed inputs for an array.
What it assumes
Assumes familiarity with Razor Pages, HTML forms, and handler methods while explaining how properties participate in model binding.
What you can do after
Apply it in Razor Pages to bind properties from forms or GET requests and adjust names for nested or array values.
How it connects
Connects model binding with Razor Pages, handler methods, Tag Helpers, nested complex types, and array binding.

Also explained in

Compare how 2 books explain BindProperty attribute →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Pro ASP.NET Core 7, Tenth Edition. The section links point to the parts of the book it draws on.