16 Binding and validating requests with Razor Pages

 

This chapter covers

  • Using request values to create binding models
  • Customizing the model-binding process
  • Validating user input using DataAnnotations attributes

In chapter 7 we looked at the process of model binding and validation in minimal APIs. In this chapter we look at the Razor Pages equivalent: extracting values from a request using model binding and validating user input.

In the first half of this chapter, we look at using binding models to retrieve those parameters from the request so that you can use them in your Razor Pages by creating C# objects. These objects are passed to your Razor Page handlers as method parameters or are set as properties on your Razor Page PageModel.

Once your code is executing in a page handler method, you can’t simply use the binding model without any further thought. Any time you’re using data provided by a user, you need to validate it! The second half of the chapter focuses on how to validate your binding models with Razor Pages.

We covered model binding and validation for minimal APIs in chapter 7, and conceptually, binding and validation are the same for Razor Pages. However, the details and mechanics of both binding and validation are quite different for Razor Pages.

16.1 Understanding the models in Razor Pages and MVC

16.2 From request to binding model: Making the request useful

16.2.1 Binding simple types

16.2.2 Binding complex types

16.2.3 Choosing a binding source

16.3 Validating binding models

16.3.1 Validation in Razor Pages

16.3.2 Validating on the server for safety

16.3.3 Validating on the client for user experience

16.4 Organizing your binding models in Razor Pages

Summary

sitemap