liveBook · Manning

Input model

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

An input model is a nested application class that groups the values a Razor Page receives through model binding.

An input model is a nested class inside a Razor Page's PageModel. It groups the values that model binding should populate from an incoming request, keeping page-specific input together. section 16.2 section 16.4

Use the pattern consistently when a page receives more than one value:

  • Expose one property, commonly named Input, and decorate it with [BindProperty]. section 16.4
  • Define InputModel as a nested class because its values are normally specific to one page. section 16.4
  • Let ASP.NET Core populate the model from form values, route values, or query-string values; binding uses the first matching value it finds. section 16.2
  • Validate the input before using it, and avoid [BindProperties], which binds every page property and can increase overposting risk. section 16.4

A page-specific input model

An edit page can expose [BindProperty] public InputModel Input { get; set; }, then place fields such as Category, Username, and ProductModel inside the nested InputModel class. The handler can accept a simple route parameter such as id directly. section 16.2 section 16.4

public class IndexModel: PageModel
{
   [BindProperty]                    
   public InputModel Input { get; set; }
   public void OnGet()
   {
   }

   public class InputModel
       {
       public string Category { get; set; }
       public string Username { get; set; }
       public ProductModel Model { get; set; }
   }
}

Inspect the compact Razor Page definition that groups several bound values inside a nested InputModel. section 16.2

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

How deep it goes
Defines the nested-class pattern, binding sources, GET behavior, and overposting caution without detailing form rendering.
Examples
Uses Category, Username, ProductModel, and an edit-product page with an id route parameter.
What you can do after
Apply it via ASP.NET Core Razor Pages to group request values and bind them through one Input property.
How it connects
Connects model binding with Razor Pages, PageModel handlers, route values, validation, and overposting attacks.

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.