Learn

When you want to take your website to the next level, you need to do more than display text and images. You need to handle data and route requests. That’s where page models come in!

Let’s review some points about ASP.NET Core and the page-focused Razor Pages framework:

  • In view pages (.cshtml files), Razor syntax lets us mix C# and HTML code in one file so that we can make flexible templates.
  • In page models (.cshtml.cs files), we use C# code and special PageModel classes to maintain data and handle different actions, like GET and POST requests.

This lesson will focus on page models and some of the things we can do with them, such as:

  • Respond to requests using the OnGet() and OnPost() methods
  • Respond to requests using the asynchronous OnGetAsync() and OnPostAsync() methods
  • Use model binding to share data between view pages and page models

Instructions

By the end of this lesson, you’ll understand nearly all of the code used in this page model. Run the code to see it working.

Remember that, by default, the name of the file determines its URL segment. Check that Index.cshtml matches localhost:8000/and Archive.cshtml matches localhost:8000/Archive.

Sign up to start coding

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?