9 SportsStore: Completing the cart

 

This chapter covers

  • Updating the shopping cart so that it persists itself as session data
  • Creating a shopping cart summary widget using a view component
  • Receiving and validating user data
  • Displaying data validation errors to the user

In this chapter, I continue to build the SportsStore example app. In the previous chapter, I added the basic support for a shopping cart, and now I am going to improve on and complete that functionality.

Tip

You can download the example project for this chapter—and for all the other chapters in this book—from https://github.com/ManningBooks/pro-asp.net-core-7. See chapter 1 for how to get help if you have problems running the examples.

9.1 Refining the cart model with a service

I defined a Cart model class in the previous chapter and demonstrated how it can be stored using the session feature, allowing the user to build up a set of products for purchase. The responsibility for managing the persistence of the Cart class fell to the Cart Razor Page, which has to deal with getting and storing Cart objects as session data.

The problem with this approach is that I will have to duplicate the code that obtains and stores Cart objects in any other Razor Page or controller that uses them. In this section, I am going to use the services feature that sits at the heart of ASP.NET Core to simplify the way that Cart objects are managed, freeing individual components from needing to deal with the details directly.

9.1.1 Creating a storage-aware cart class

9.1.2 Registering the service

9.1.3 Simplifying the cart Razor Page

9.2 Completing the cart functionality

9.2.1 Removing items from the cart

9.2.2 Adding the cart summary widget

9.3 Submitting orders

9.3.1 Creating the model class

9.3.2 Adding the checkout process

9.3.3 Creating the controller and view

9.3.4 Implementing order processing

9.3.5 Completing the order controller

9.3.6 Displaying validation errors

9.3.7 Displaying a summary page

Summary