Create an E-Commerce Application in ASP.NET 5 MVC – Part 1

Table of Contents
In this part, we shall create an e-commerce product shopping cart application with the following actions:
Create a New Project
Open up Visual Studio Community 2019 IDE. Click on Create a new project from Get started

On the Create a new project dialog click on ASP.NET Core Web Application. Click Next

On the Configure your new project dialog give a suitable Project name (like ECommerceShoppingCartASPNet5) and location and click Create
You may leave the checkbox at the bottom (Place solution and project in the same directory) unchecked

On Create a new ASP.NET Core web application, select ASP.NET Core Web App(Model-View-Controller) template and leave the other selections as per the below fig. (Ensure that ASP.NET Core 5.0 is selected)

When the project is loaded, bring up the Solution Explorer window on the right hand. Expand the Controllers folder and highlight the default HomeController and delete it.
Now Go to the folder Views => Home and delete the Home folder (as it is no more required since we have already removed the HomeController
Add a Product Controller
In the Controllers folder, add an empty MVC Controller by right clicking Controllers folder and Add => Controller

On the Add New Scaffolded Item dialog, select MVC Controller – Empty and click Add

On Add New Item name the Controller as ProductController and click Add

Create a Product Model
Now to create a Product model, right click the default Models folder and add a Product class in a new file
Replace the default code in the Product class with the following
public class Product
{
public string Id { get; set; }
public string Name { get; set; }
public double Price { get; set; }
public string Photo { get; set; }
}
Create an Item class in the Models folder and replace the code in the Item class with the following
public class Item
{
public Product Product { get; set; }
public int Quantity { get; set; }
}
Create a View Model for Product
Now create a folder on the root level of the project and name this folder ViewModels
Add a ProductModel class in the ViewModels folder with the following code
public class ProductModel
{
public List<Product> _products { get; set; }
public List<Product> findAll()
{
_products = new List<Product>{new Product()
{
Id = "1", Name = "Flower1", Photo = "thumb1.jpg", Price = 2.80
},
new Product()
{
Id = "2",Name="Flower2",Photo="thumb2.jpg", Price=3.80
},
new Product()
{
Id = "3",Name="Flower3",Photo="thumb3.jpg", Price=3.80
},
new Product()
{
Id = "4",Name="Flower4",Photo="thumb4.jpg", Price=3.60
},
new Product()
{
Id = "5",Name="Flower5",Photo="thumb5.jpg", Price=6.80
},
new Product()
{
Id = "6",Name="Flower6",Photo="thumb6.jpg", Price=1.68
},
new Product()
{
Id = "7",Name="Flower7",Photo="thumb7.jpg", Price=3.80
},
new Product()
{
Id = "8",Name="Flower8",Photo="thumb8.jpg", Price=3.45
},
new Product()
{
Id = "9",Name="Flower9",Photo="thumb9.jpg", Price=3.80
},
new Product()
{
Id = "10",Name="Flower10",Photo="thumb10.jpg", Price=3.80
}
};
return _products;
}
public Product find(string id)
{
List<Product> products = findAll();
var prod = products.Where(a => a.Id == id).FirstOrDefault();
return prod;
}
}
The findAll() method initialises a list of Products setting values to the properties of the Product model. It then returns the list.
find(..) method returns a Product with a specified id it receives as a method parameter.
Now include the Models folder in the ProductModel class to remove any compilation error and build the solution once to check that there are no runtime errors.
Create an images folder within wwwroot and place some images (I included some flower images) and a Buy Now stock image as per the Fig. below:

Copy the following code to replace the existing code for the ProductController
public class ProductController : Controller
{
public IActionResult Index()
{
ProductModel productModel = new ProductModel();
ViewBag.products = productModel.findAll();
return View();
}
}
At this stage, make sure that the file _Layout.cshtml under Views/Shared folder contains reference to bootstrap.css file like: within the ‘head’ tag like this:<link rel=”stylesheet” href=”~/lib/bootstrap/dist/css/bootstrap.css”/>
Create a View for ProductController Index Action Method
Create an empty view for the Index() action method by right clicking Index() and selecting Add View

Then in Add New Scaffolded Item highlight Razor View – Empty and click Add

Then in Add New Item go for the default name and then Add

Replace the code in the Index View (Index.cshtml) with the following code:
@{
Layout = "_Layout";
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<h3>Products List</h3>
<table cellpadding="2" cellspacing="2" border="1">
<tr>
<th>Id</th>
<th>Name</th>
<th>Photo</th>
<th>Price</th>
<th>Buy</th>
</tr>
@foreach (var product in ViewBag.products)
{
<tr>
<td class="d-lg-table-cell">@product.Id</td>
<td class="d-lg-table-cell">@product.Name</td>
<td class="d-lg-table-cell"><img src="~/images/@product.Photo" width="60" /> </td>
<td class="d-lg-table-cell">@(Math.Round(product.Price, 2))</td>
<td align="center">
<a asp-controller="cart" asp-action="buy" asp-route-id="@product.Id"> <img src="~/images/BuyNow.jpg" width="100" /></a>
</td>
</tr>
}
</table>
</body>
</html>
The foreach…. loop above loops through all the products in the ViewBag and uses a bootstrap class for styling. The <img /> tag assigns the images of the flower and Buy Now icon. I have used Math.Round() to round up the price to 2 decimal places
Configure Startup
In the Startup class Configure method change the code for the Endpoints middleware at the end with the following code (to replace the Home controller with Product
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Product}/{action=Index}/{id?}");
});
Run the Application
Finally run the application by clicking the IIS Express icon and you’ll see

However, if you click the Buy Now button, nothing will come up since we don’t have the cart as yet as below. It will be taken up in part 2.
