This project showcases a simple ASP.NET MVC project which consists of two controller classes with functionality for:
- displaying a list of products with their price and a link for adding to basket
- storing the contents of the shopping cart in the visitor's browser
- displaying the content of the cart including the price for single orderlines and the total sum
- adding/removing items and emptying both a single product type or the entire basket
The ShopController is just a list of products with links to the CartController for adding or adjusting the contents of the cart.
The cart is based on two elements, the CartController class and the Cart model class:
The CartController is an ASP.NET MVC controller. It has a reference to a DAL class where it retrieves the products to display, and uses the Cart class to retrieve and save the contents of the cart.
In a real webshop the CartController would also have a reference to a DAL object to place the order.
The Cart is basically a wrapper around a C# Dictionary collection which stores ProductQuantity helperobjects (value) under the ID of products (key), for easy retrival and look ups. It also serializes and deserializes the cart to and from JSON for storage in a cookie.
The ProductQuantity objects are simple objects with only the necessary data to display a product's name, the price, the total price and store the ID of the product. The ProductQuantity has a constructor which receives a Product object and stores the data from the product:
public ProductQuantity(Product product, int quantity)
{
Id = product.Id;
Price = product.Price;
Name = product.Name;
Quantity = quantity;
}...you may have to change this to your product objects (or DTOs), in order to make it work with the product objects in your data access layer.
The CartController can be imported into your project and used as is.
You will need to add the following files to your project
In your products page you should add links to the CartController in the format: /Cart/Add/6?quantity=1 where the 6 is the ID of your product and the quantity is how many to add.
If the item wasn't in the cart, this will add an item to the cart in the requested quantity.
If the item is in the cart already, this will increase(or decrease) the quantity in the cart.
The cart only stores a minimum of info about the products in order to display them.
- Id (primary key)
- Name
- Price
- Quantity
This is done in order to avoid having to request this information from the data source (database/API/etc.) every time the cart is displayed.
You have to provide the CartController with a way to get this productinfo, when items are added to the cart, so ProductQuantity objects can be created and stored in the cart.
In the code sample an IProductDao is used, but the only necessary code, for the cart to work, is a method which can retrieve a product object given an Id, so the properties from that product can be copied to a ProductQuantity object.

