chapter five

5 Working with forms - model binding

 

This chapter covers

  • Model binding in Razor Pages
  • Binding complex objects easily
  • Protecting your application by validating user input
  • Leveraging data annotation attributes to manage validation

Right at the beginning of this book, I congratulated you for choosing Razor Pages In Action if you wanted to learn how to build “page-focused interactive web applications”. I didn’t really expand on the meaning of “interactive” at the time. Essentially, an interactive web application is one where users can provide input and affect the behaviour of the application. In the last chapter, you saw how a user can alter a URL to interact with the application, causing the City page to display different content based on the value of a route parameter. In this chapter, you get to understand and begin to work with the primary interaction mechanism in a web application - the form.

5.1  Forms Basics

5.1.1   Using the Post-Redirect-Get pattern for handling forms

5.1.2   Accessing the submitted values from Request.Form

5.1.3   Accessing the submitted values from Request.Query

5.2  Model Binding

5.2.1   Using model binding with handler parameters

5.2.2   Using model binding with public properties

5.2.3   Binding Complex Objects

5.2.4   Binding Simple Collections

5.2.5   Binding Complex Collections

5.3  Validating User Input in Razor Pages

5.3.1   DataAnnotation Attributes

5.3.2   Client side validation

5.3.3   Server side validation

5.3.4   Managing More Complex Validation with ModelState

5.3.5   Custom Validation Attributes

5.4  Summary