ID
using a globally unique identifier (GUID) when the user accesses the shopping cart for the first time. You'll store this ID
using the ASP.NET Session state.Category
and Product
classes in the Models folder. Now, add a new class to define the schema for the shopping cart. Later in this tutorial, you will add a class to handle data access to the CartItem
table. This class will provide the business logic to add, remove, and update items in the shopping cart.CartItem
class contains the schema that will define each product a user adds to the shopping cart. This class is similar to the other schema classes you created earlier in this tutorial series. By convention, Entity Framework Code First expects that the primary key for the CartItem
table will be either CartItemId
or ID
. However, the code overrides the default behavior by using the data annotation [Key]
attribute. The Key
attribute of the ItemId property specifies that the ItemID
property is the primary key.CartId
property specifies the ID
of the user that is associated with the item to purchase. You'll add code to create this user ID
when the user accesses the shopping cart. This ID
will also be stored as an ASP.NET Session variable.CartItem
class, you will need to update the database context class that manages the entity classes and that provides data access to the database. To do this, you will add the newly created CartItem
model class to the ProductContext
class.System.Data.Entity
namespace so that you have access to all the core functionality of the Entity Framework. This functionality includes the capability to query, insert, update, and delete data by working with strongly typed objects. The ProductContext
class adds access to the newly added CartItem
model class.ShoppingCart
class in a new Logic folder. The ShoppingCart
class handles data access to the CartItem
table. The class will also include the business logic to add, remove, and update items in the shopping cart.ShoppingCart
class will be added to a separate folder in the application so that there will be a clear distinction between the model (Models folder), the pages (root folder) and the logic (Logic folder).AddToCart
method enables individual products to be included in the shopping cart based on the product ID
. The product is added to the cart, or if the cart already contains an item for that product, the quantity is incremented.GetCartId
method returns the cart ID
for the user. The cart ID
is used to track the items that a user has in their shopping cart. If the user does not have an existing cart ID
, a new cart ID
is created for them. If the user is signed in as a registered user, the cart ID
is set to their user name. However, if the user is not signed in, the cart ID
is set to a unique value (a GUID). A GUID ensures that only one cart is created for each user, based on session.GetCartItems
method returns a list of shopping cart items for the user. Later in this tutorial, you will see that model binding is used to display the cart items in the shopping cart using the GetCartItems
method.AddToCart
method in the ShoppingCart
class that you just created. The AddToCart.aspx page will expect that a product ID
is passed to it. This product ID
will be used when calling the AddToCart
method in the ShoppingCart
class.ID
is retrieved from the query string. Next, an instance of the shopping cart class is created and used to call the AddToCart
method that you added earlier in this tutorial. The AddToCart
method, contained in the ShoppingCartActions.cs file, includes the logic to add the selected product to the shopping cart or increment the product quantity of the selected product. If the product hasn't been added to the shopping cart, the product is added to the CartItem
table of the database. If the product has already been added to the shopping cart and the user adds an additional item of the same product, the product quantity is incremented in the CartItem
table. Finally, the page redirects back to the ShoppingCart.aspx page that you'll add in the next step, where the user sees an updated list of items in the cart.ID
is used to identify the products that are associated with a specific user. This ID
is added to a row in the CartItem
table each time the user adds a product to the shopping cart.CartList
. This control uses model binding to bind the shopping cart data from the database to the GridView control. When you set the ItemType
property of the GridView control, the category.GetTotal
method to the ShoppingCart
class and display the total order amount in the shopping cart page.GetTotal
method highlighted in yellow to the ShoppingCart
class, so that the class appears as follows:GetTotal
method gets the ID of the shopping cart for the user. Then the method gets the cart total by multiplying the product price by the product quantity for each product listed in the cart.int?
'. Nullable types can represent all the values of an underlying type, and also as a null value. For more information see, Using Nullable Types.GetTotal
method and display that total on the ShoppingCart.aspx page when the page loads.Page_Load
handler by adding the following code highlighted in yellow:GetTotal
method of the ShoppingCart
class. If the shopping cart is empty, a message to that effect is displayed.UpdateBtn_Click
event handler will be called. This event handler will call the code that you'll add in the next step.RemoveItem
and UpdateItem
methods.UpdateShoppingCartDatabase
method (added and explained in the next step) to either add or remove items from the shopping cart. Once the database has been updated to reflect the updates to the shopping cart, the GridView control is updated on the shopping cart page by calling the DataBind
method for the GridView. Also, the total order amount on the shopping cart page is updated to reflect the updated list of items.UpdateShoppingCartDatabase
method, called from the UpdateCartItems
method on the ShoppingCart.aspx.cs page, contains the logic to either update or remove items from the shopping cart. The UpdateShoppingCartDatabase
method iterates through all the rows within the shopping cart list. If a shopping cart item has been marked to be removed, or the quantity is less than one, the RemoveItem
method is called. Otherwise, the shopping cart item is checked for updates when the UpdateItem
method is called. After the shopping cart item has been removed or updated, the database changes are saved.ShoppingCartUpdates
structure is used to hold all the shopping cart items. The UpdateShoppingCartDatabase
method uses the ShoppingCartUpdates
structure to determine if any of the items need to be updated or removed.EmptyCart
method to clear the shopping cart after purchasing products. But for now, you will use the GetCount
method that you just added to the ShoppingCartActions.cs file to determine how many items are in the shopping cart.Page_PreRender
event is raised. In the Page_PreRender
handler, the total count of the shopping cart is determined by calling the GetCount
method. The returned value is added to the cartCount
span included in the markup of the Site.Master page. The <span>
tags enables the inner elements to be properly rendered. When any page of the site is displayed, the shopping cart total will be displayed. The user can also click the shopping cart total to display the shopping cart.