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

At the beginning of this book, I congratulated you for choosing ASP.NET Core 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 in which users can provide input and affect the behavior 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 will begin to understand and 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

5.1.2 Accessing values from Request.Form

5.1.3 Accessing 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