liveBook · Manning

Chapter 16: Binding and validating requests with Razor Pages

What you will learn in chapter 16 of ASP.NET Core in Action, 3e

This chapter introduces Razor Pages model binding and validation, framing how request data becomes binding models and must be validated before use.

After this chapter you will be able to

What it builds on

Section by section

Introduction

This chapter introduces Razor Pages model binding and validation, framing how request data becomes binding models and must be validated before use.

Key terms: model binding, binding models, page handlers, PageModel, Razor Pages infrastructure, validation, DataAnnotations attributes, user input, view models, API models

16.1 Understanding the models in Razor Pages and MVC

This section distinguishes the binding model, application model, and PageModel in Razor Pages and explains how their separate responsibilities fit into ASP.NET Core’s MVC request flow.

Key terms: binding model, application model, PageModel, page handler, view model, MVC, route parameters, query string, request body, form data

16.2 From request to binding model: Making the request useful

This section explains how Razor Pages model binding turns request data into properties or handler arguments, where those values come from, and how to organize binding targets.

Key terms: model binding, binding model, PageModel, page handler, [BindProperty], [BindProperties], SupportsGet, binding sources, form values, route values

16.2.1 Binding simple types

This section explains how Razor Pages model binding supplies simple page-handler parameters from request data and what happens when values are missing or conflict.

Key terms: model binding, binding model, page handler, route values, binding sources, form values, query string values, model binder, default(T), nullable types

16.2.2 Binding complex types

This section explains how Razor Pages model binding populates complex objects, collections, dictionaries, and uploaded-file types from request data.

Key terms: binding model, PageModel, page handler, [BindProperty], model binder, binding sources, public settable properties, default public constructor, List<string>, collections

16.2.3 Choosing a binding source

This section explains how Razor Pages chooses model-binding sources by default and how attributes explicitly bind handler parameters or properties to request data.

Key terms: model binder, binding models, form data, route data, query string, [FromHeader], [FromQuery], [FromRoute], [FromForm], [FromBody]

16.3 Validating binding models

This section introduces built-in Razor Pages validation for binding models using the DataAnnotations approach already covered for user input.

Key terms: Razor Pages, binding models, validation, DataAnnotations attributes

16.3.1 Validation in Razor Pages

This section explains Razor Pages' built-in validation pipeline, why validation must run on the server, and how binding models declare validation rules.

Key terms: model binding, validation, page handler, binding model, client-side validation, server-side validation, business requirements, DataAnnotations, validation attributes, IValidatableObject

16.3.2 Validating on the server for safety

This section explains how Razor Page handlers safely handle server-side validation failures and use POST-REDIRECT-GET after successful form submissions.

Key terms: ModelState, ModelStateDictionary, ModelState.IsValid, PageModel, page handler, Page(), PageResult, Input, ErrorMessage, DataAnnotation

16.3.3 Validating on the client for user experience

This section explains how browser and JavaScript client-side validation improves form feedback while server-side validation remains essential.

Key terms: client-side validation, server-side validation, user experience, HTML5 constraint validation, email input type, JavaScript, DataAnnotation attributes, view model, Razor engine, validation HTML

16.4 Organizing your binding models in Razor Pages

This section recommends a consistent, safer convention for organizing Razor Pages binding models, route values, and validation.

Key terms: [BindProperty], InputModel, model binding, PageModel, nested class, binding model, [BindProperties], overposting attacks, route parameters, OnGet

16.5 Summary

This summary recaps Razor Pages models, request model binding sources and rules, and the need to validate bound input on both server and client.

Where this is used later

Concepts explained in this chapter

← Chapter 15: Generating responses with page handlers in Razor Pages Chapter 17: Rendering HTML using Razor views →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.