Introduction

In this article, let’s see how to create a shopping cart using ASP.NET Core, Angular 2, Entity Framework 1.0.1, and Web API with Template pack .

Note

Kindly read my previous articles which explain in-depth about getting started with ASP.NET Core Template Pack.

In this article, we will learn about:.

This article will explain how to create a simple shopping cart using ASP.NET Core, Angular 2, Web API and EF with Template Pack.

In this shopping cart demo application, we have 3 parts.

Prerequisites

Make sure you have installed all the prerequisites on your computer. If not, then download and install all of them, one by one.

  1. First, download and install Visual Studio 2015 with Update 3 from this link.
  2. If you have Visual Studio 2015 and have not yet updated with update 3, download and install the Visual Studio 2015 Update 3 from this link.
  3. Download and install .NET Core 1.0.1
  4. Download and install TypeScript 2.0
  5. Download and install Node.js v4.0 or above. I have installed V6.9.1 (Download link).
  6. Download and install Download ASP.NET Core Template Pack visz file from this link.

Code Part

Step 1 Create a Database and Table

We will create "ItemDetails" table to be used for the Shopping Cart Grid data binding. The following is the script to create a database, table, and sample insert query.

Run this script in your SQL Server. I have used SQL Server 2014.

  1. USE MASTER
  2. GO
  3. -- 1) Check for the Database Exists .If the database is exist then drop and create new DB
  4. IF EXISTS (SELECT [name] FROM sys.databases WHERE [name] = 'ShoppingDB' )
  5. DROP DATABASE ShoppingDB
  6. GO
  7. CREATE DATABASE ShoppingDB
  8. GO
  9. USE ShoppingDB
  10. GO
  11. -- 1) //////////// ItemDetails table
  12. -- Create Table ItemDetails,This table will be used to store the details like Item Information
  13. IF EXISTS ( SELECT [name] FROM sys.tables WHERE [name] = 'ItemDetails' )
  14. DROP TABLE ItemDetails
  15. GO
  16. CREATE TABLE ItemDetails
  17. (
  18. Item_ID int identity(1,1),
  19. Item_Name VARCHAR(100) NOT NULL,
  20. Item_Price int NOT NULL,
  21. Image_Name VARCHAR(100) NOT NULL,
  22. Description VARCHAR(100) NOT NULL,
  23. AddedBy VARCHAR(100) NOT NULL,
  24. CONSTRAINT [PK_ItemDetails] PRIMARY KEY CLUSTERED
  25. (
  26. [Item_ID] ASC
  27. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
  28. ) ON [PRIMARY]
  29. GO
  30. -- Insert the sample records to the ItemDetails Table
  31. Insert into ItemDetails(Item_Name,Item_Price,Image_Name,Description,AddedBy) values('Access Point',950,'AccessPoint.png','Access Point for Wifi use','Shanu')
  32. Insert into ItemDetails(Item_Name,Item_Price,Image_Name,Description,AddedBy) values('CD',350,'CD.png','Compact Disk','Afraz')
  33. Insert into ItemDetails(Item_Name,Item_Price,Image_Name,Description,AddedBy) values('Desktop Computer',1400,'DesktopComputer.png','Desktop Computer','Shanu')
  34. Insert into ItemDetails(Item_Name,Item_Price,Image_Name,Description,AddedBy) values('DVD',1390,'DVD.png','Digital Versatile Disc','Raj')
  35. Insert into ItemDetails(Item_Name,Item_Price,Image_Name,Description,AddedBy) values('DVD Player',450,'DVDPlayer.png','DVD Player','Afraz')
  36. Insert into ItemDetails(Item_Name,Item_Price,Image_Name,Description,AddedBy) values('Floppy',1250,'Floppy.png','Floppy','Mak')
  37. Insert into ItemDetails(Item_Name,Item_Price,Image_Name,Description,AddedBy) values('HDD',950,'HDD.png','Hard Disk','Albert')
  38. Insert into ItemDetails(Item_Name,Item_Price,Image_Name,Description,AddedBy) values('MobilePhone',1150,'MobilePhone.png','Mobile Phone','Gowri')
  39. Insert into ItemDetails(Item_Name,Item_Price,Image_Name,Description,AddedBy) values('Mouse',399,'Mouse.png','Mouse','Afraz')
  40. Insert into ItemDetails(Item_Name,Item_Price,Image_Name,Description,AddedBy) values('MP3 Player ',897,'MultimediaPlayer.png','Multi MediaPlayer','Shanu')
  41. Insert into ItemDetails(Item_Name,Item_Price,Image_Name,Description,AddedBy) values('Notebook',750,'Notebook.png','Notebook','Shanu')
  42. Insert into ItemDetails(Item_Name,Item_Price,Image_Name,Description,AddedBy) values('Printer',675,'Printer.png','Printer','Kim')
  43. Insert into ItemDetails(Item_Name,Item_Price,Image_Name,Description,AddedBy) values('RAM',1950,'RAM.png','Random Access Memory','Jack')
  44. Insert into ItemDetails(Item_Name,Item_Price,Image_Name,Description,AddedBy) values('Smart Phone',679,'SmartPhone.png','Smart Phone','Lee')
  45. Insert into ItemDetails(Item_Name,Item_Price,Image_Name,Description,AddedBy) values('USB',950,'USB.png','USB','Shanu')
  46. select * from ItemDetails

Step 2- Create ASP.NET Core Angular 2 application


After creating ASP.NET Core Angular 2 application, wait for a few seconds. You will see that all the dependencies are automatically restored.

We will be using all these in our project to create, build, and run our Angular 2 with ASP.NET Core Template Pack, Web API, and EF 1.0.1.

Step 3 Creating Entity Framework

Add Entity Framework Packages

To add our Entity Framework Packages in our ASP.NET Core application, open the Project.JSON file and in dependencies add the below line.

Note

Here, we have used EF version 1.0.1.

  1. "Microsoft.EntityFrameworkCore.SqlServer": "1.0.1",
  2. "Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final"

When we save the project,.json file, we can see that the Reference has been restored.

After a few seconds, we can see Entity Framework package has been restored and all references have been added.

Adding Connection String

To add the connection string with our SQL connection, open the “appsettings.json” file .Yes, this is a JSON file and this file looks like the below image by default.

In this appsettings.json file, add the connection string.

  1. "ConnectionStrings": {
  2. "DefaultConnection": "Server=YOURDBSERVER;Database=StudentsDB;user id=SQLID;password=SQLPWD;Trusted_Connection=True;MultipleActiveResultSets=true;"
  3. },

Note - change the SQL connection string as per your local connection.

The next step is to create a folder named “Data” to create our model and DBContext class.

Creating Model Class for Student Master

We can create a model by adding a new class file in our "Data" folder. Right click "Data" folder and click Add >> Class. Enter the class name as itemDetails and click "Add".

Now, in this class, we first create property variable, add ItemDetails. We will be using this in our Web API Controller.

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using System.ComponentModel.DataAnnotations;
  6. namespace Angular2ASPCORE.Data
  7. {
  8. public class ItemDetails
  9. {
  10. [Key]
  11. public int Item_ID { get; set; }
  12. [Required]
  13. [Display(Name = "Item_Name")]
  14. public string Item_Name { get; set; }
  15. [Required]
  16. [Display(Name = "Item_Price")]
  17. public int Item_Price { get; set; }
  18. [Required]
  19. [Display(Name = "Image_Name")]
  20. public string Image_Name { get; set; }
  21. [Required]
  22. [Display(Name = "Description")]
  23. public string Description { get; set; }
  24. [Required]
  25. [Display(Name = "AddedBy")]
  26. public string AddedBy { get; set; }
  27. }
  28. }

Creating Database Context

DBContext is Entity Framework Class for establishing connection to database.

We can create a DBContext class by adding a new class file in our Data folder. Right click Data folder and click Add >> Class. Enter the class name as ItemContext and click "Add".

In this class, we inherit DbContext and create Dbset for our ItemDetails table.

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using Microsoft.EntityFrameworkCore;
  6. namespace Angular2ASPCORE.Data
  7. {
  8. public class ItemContext : DbContext
  9. {
  10. public ItemContext(DbContextOptions<ItemContext> options)
  11. : base(options) { }
  12. public ItemContext() { }
  13. public DbSet<ItemDetails> ItemDetails { get; set; }
  14. }
  15. }

Startup.CS

Now, we need to add our database connection string and provider as SQL SERVER.To add this, we add the below code in Startup.cs file under ConfigureServices method.

  1. // Add Entity framework .
  2. services.AddDbContext<studentContext>(options =>
  3. options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

Step 4 Creating Web API

To create our Web API Controller, right click "Controllers" folder. Click Add >> New Item.

Click ASP.NET in right side >> Click Web API Controller Class. Enter the name as “itemDetailsAPI.cs” and click "Add".

In this, we are using only Get method to get all the ItemDetails result from database and binding the final result using Angular 2 to HTML file.

Here, in this Web API, we get all ItemDetails and ItemDetails loaded by condition ItemName.

  1. [Produces("application/json")]
  2. [Route("api/ItemDetailsAPI")]
  3. public class ItemDetailsAPI : Controller
  4. {
  5. private readonly ItemContext _context;
  6. public ItemDetailsAPI(ItemContext context)
  7. {
  8. _context = context;
  9. }
  10. // GET: api/values
  11. [HttpGet]
  12. [Route("Details")]
  13. public IEnumerable<ItemDetails> GetItemDetails()
  14. {
  15. return _context.ItemDetails;
  16. }
  17. // GET api/values/5
  18. [HttpGet]
  19. [Route("Details/{ItemName}")]
  20. public IEnumerable<ItemDetails> GetItemDetails(string ItemName)
  21. {
  22. //return _context.ItemDetails.Where(i => i.Item_ID == id).ToList(); ;
  23. return _context.ItemDetails.Where(i => i.Item_Name.Contains(ItemName)).ToList();
  24. }

To test it, we can run our project and copy the get method API path. Here, we can see that our API path for get is /api/ItemDetailsAPI/Details

Run the program and paste the above API path to test our output.

To get the Item Details by ItemName. Here, we can see all the ItemDetails which start from ItemName “DVD” has been loaded.

/api/ItemDetailsAPI/Details/DVD

Working with Angular 2

Create all Angular 2 related Apps, Modules, Services, Components, and HTML templates under ClientApp/App folder.

We need to create “model” folder adding our models and create “shopping” folder under app folder to create our TypeScript and HTML file for displaying Item details.

Note - Images Folder

First create a folder called “Images” inside the "Shopping" folder. I have used this folder to display all shopping cart images. If you store shopping image in some other path in your code, change accordingly.

Step 5 Creating our First Component TypeScript

Right click on Shopping folder and click on add new Item. Select client-side from left side and select TypeScript file and name the file as “shopping.component.ts” and click "Add".

In students.component.ts file, we have three parts -

  1. import part
  2. component part
  3. class for writing our business logic.

First, import Angular files to be used in our component; here, we import HTTP for using HTTP client in our Angular 2 component.

In component, we have selector and template. Selector is to give a name for this app and in our HTML file, we can use this selector name to display in our HTML page.

In template.give your output html file name. Here, we will create one HTML file as “students.component.html”.

Export Class is the main class where we perform all our business logic and variable declaration to be used in our component template. In this class, we get the API method result and bind the result to the student array.

Here, in the code part, I have commented each section for easy understanding.

  1. import { Component, Injectable, Inject, EventEmitter, Input, OnInit, Output, NgModule } from '@angular/core';
  2. import { FormsModule } from '@angular/forms';
  3. import { ActivatedRoute, Router } from '@angular/router';
  4. import { BrowserModule } from '@angular/platform-browser';
  5. import { Http,Headers, Response, Request, RequestMethod, URLSearchParams, RequestOptions } from "@angular/http";
  6. import { ItemDetails } from '../model/ItemDetails';
  7. import { CartItemDetails } from '../model/CartItemDetails';
  8. @Component({
  9. selector: 'shopping',
  10. template: require('./shopping.component.html')
  11. })
  12. export class shoppingComponent {
  13. //Declare Variables to be used
  14. //To get the WEb api Item details to be displayed for shopping
  15. public ShoppingDetails: ItemDetails[] = [];
  16. myName: string;
  17. //Show the Table row for Items,Cart and Cart Items.
  18. showDetailsTable: Boolean = true;
  19. AddItemsTable: Boolean = false;
  20. CartDetailsTable: Boolean = false;
  21. public cartDetails: CartItemDetails[] = [];
  22. public ImageUrl = require("./Images/CD.png");
  23. public cartImageUrl = require("./Images/shopping_cart64.png");
  24. //For display Item details and Cart Detail items
  25. public ItemID: number;
  26. public ItemName: string = "";
  27. public ItemPrice: number = 0;
  28. public Imagename: string = "";
  29. public ImagePath: string = "";
  30. public Descrip: string = "";
  31. public txtAddedBy: string = "";
  32. public Qty: number = 0;
  33. //For calculate Total Price,Qty and Grand Total price
  34. public totalPrice: number = 0;
  35. public totalQty: number = 0;
  36. public GrandtotalPrice: number = 0;
  37. public totalItem: number = 0;
  38. //Inital Load
  39. constructor(public http: Http) {
  40. this.myName = "Shanu";
  41. this.showDetailsTable = true;
  42. this.AddItemsTable = false;
  43. this.CartDetailsTable = false;
  44. this.getShoppingDetails('');
  45. }
  46. //Get all the Item Details and Item Details by Item name
  47. getShoppingDetails(newItemName) {
  48. if (newItemName == "") {
  49. this.http.get('/api/ItemDetailsAPI/Details').subscribe(result => {
  50. this.ShoppingDetails = result.json();
  51. });
  52. }
  53. else {
  54. this.http.get('/api/ItemDetailsAPI/Details/' + newItemName).subscribe(result => {
  55. this.ShoppingDetails = result.json();
  56. });
  57. }
  58. }
  59. //Get Image Name to bind
  60. getImagename(newImage) {
  61. this.ImageUrl = require("./Images/" + newImage);
  62. }
  63. // Show the Selected Item to Cart for add to my cart Items.
  64. showToCart(Id, Name, Price, IMGNM, Desc,user)
  65. {
  66. this.showDetailsTable = true;
  67. this.AddItemsTable = true;
  68. this.CartDetailsTable = false;
  69. this.ItemID = Id;
  70. this.ItemName = Name;
  71. this.ItemPrice = Price;
  72. this.Imagename = require("./Images/" + IMGNM);
  73. this.ImagePath = IMGNM
  74. this.Descrip = Desc;
  75. this.txtAddedBy = user;
  76. }
  77. // to Show Items to be added in cart
  78. showCart() {
  79. this.showDetailsTable = false;
  80. this.AddItemsTable = true;
  81. this.CartDetailsTable = true;
  82. this.addItemstoCart();
  83. }
  84. // to show all item details
  85. showItems() {
  86. this.showDetailsTable = true;
  87. this.AddItemsTable = false;
  88. this.CartDetailsTable = false;
  89. }
  90. //to Show our Shopping Items details
  91. showShoppingItems() {
  92. if (this.cartDetails.length <= 0)
  93. {
  94. alert("Ther is no Items In your Cart.Add Items to view your Cart Details !")
  95. return;
  96. }
  97. this.showDetailsTable = false;
  98. this.AddItemsTable = false;
  99. this.CartDetailsTable = true;
  100. }
  101. //Check the Item already exists in Cart,If the Item is exist then add only the quantity else add selected item to cart.
  102. addItemstoCart() {
  103. var count: number = 0;
  104. var ItemCountExist: number = 0;
  105. this.totalItem = this.cartDetails.length;
  106. if (this.cartDetails.length > 0) {
  107. for (count = 0; count < this.cartDetails.length; count++) {
  108. if (this.cartDetails[count].CItem_Name == this.ItemName) {
  109. ItemCountExist = this.cartDetails[count].CQty + 1;
  110. this.cartDetails[count].CQty = ItemCountExist;
  111. }
  112. }
  113. }
  114. if (ItemCountExist <= 0)
  115. {
  116. this.cartDetails.push(
  117. new CartItemDetails(this.ItemID, this.ItemName, this.ImagePath, this.Descrip, this.txtAddedBy, this.ItemPrice, 1, this.ItemPrice));
  118. }
  119. this.getItemTotalresult();
  120. }
  121. //to calculate and display the total price information in Shopping cart.
  122. getItemTotalresult() {
  123. this.totalPrice = 0;
  124. this.totalQty = 0;
  125. this.GrandtotalPrice = 0;
  126. var count: number = 0;
  127. this.totalItem = this.cartDetails.length;
  128. for (count = 0; count < this.cartDetails.length; count++) {
  129. this.totalPrice += this.cartDetails[count].CItem_Price;
  130. this.totalQty += (this.cartDetails[count].CQty);
  131. this.GrandtotalPrice += this.cartDetails[count].CItem_Price * this.cartDetails[count].CQty;
  132. }
  133. }
  134. //remove the selected item from the cart.
  135. removeFromCart(removeIndex) {
  136. alert(removeIndex);
  137. this.cartDetails.splice(removeIndex, 1);
  138. this.getItemTotalresult();
  139. }
  140. }

Step 6 Creating our First Component HTML File

Right click on shopping folder and click on "Add New Item". Select client-side from left side and select HTML file and name the file as “shopping.component.html” and click "Add".

Write the below HTML code to bind the result in the HTML page to display all the "Shopping Items" and "Shopping Cart" details.

  1. <h1>{{myName}} ASP.NET Core , Angular2 Shopping Cart using Web API and EF 1.0.1 </h1>
  2. <hr style="height: 1px;color: #123455;background-color: #d55500;border: none;color: #d55500;" />
  3. <p *ngIf="!ShoppingDetails"><em>Loading Student Details please Wait ! ...</em></p>
  4. <!--<pre>{{ ShoppingDetails | json }}</pre>-->
  5. <table id="tblContainer" style='width: 99%;table-layout:fixed;'>
  6. <tr *ngIf="AddItemsTable">
  7. <td>
  8. <table style="background-color:#FFFFFF; border: dashed 3px #6D7B8D; padding: 5px;width: 99%;table-layout:fixed;" cellpadding="2"
  9. cellspacing="2">
  10. <tr style="height: 30px; color:#ff0000 ;border: solid 1px #659EC7;">
  11. <td width="40px"> </td>
  12. <td>
  13. <h2> <strong>Add Items to Cart</strong></h2>
  14. </td>
  15. </tr>
  16. <tr>
  17. <td width="40px"> </td>
  18. <td>
  19. <table>
  20. <tr>
  21. <td>
  22. <img src="{{Imagename}}" width="150" height="150" />
  23. </td>
  24. <td width="30"></td>
  25. <td valign="top">
  26. <table style="color:#9F000F;font-size:large" cellpadding="4" cellspacing="6">
  27. <tr>
  28. <td>
  29. <b>Item code </b>
  30. </td>
  31. <td>
  32. : {{ItemID}}
  33. </td>
  34. </tr>
  35. <tr>
  36. <td>
  37. <b> Item Name</b>
  38. </td>
  39. <td>
  40. : {{ItemName}}
  41. </td>
  42. </tr>
  43. <tr>
  44. <td>
  45. <b> Price </b>
  46. </td>
  47. <td>
  48. : {{ItemPrice}}
  49. </td>
  50. </tr>
  51. <tr>
  52. <td>
  53. <b> Description </b>
  54. </td>
  55. <td>
  56. : {{Descrip}}
  57. </td>
  58. </tr>
  59. <tr>
  60. <td align="center" colspan="2">
  61. <table>
  62. <tr>
  63. <td>
  64. <button (click)=showCart() style="background-color:#4c792d;color:#FFFFFF;font-size:large;width:200px">
  65. Add to Cart
  66. </button>
  67. </td>
  68. <td rowspan="2"><img src="{{cartImageUrl}}" /></td>
  69. </tr>
  70. </table>
  71. </td>
  72. </tr>
  73. </table>
  74. </td>
  75. </tr>
  76. </table>
  77. </td>
  78. </tr>
  79. </table>
  80. </td>
  81. </tr>
  82. <tr>
  83. <td><hr style="height: 1px;color: #123455;background-color: #d55500;border: none;color: #d55500;" /></td>
  84. </tr>
  85. <tr *ngIf="CartDetailsTable">
  86. <td>
  87. <table width="100%">
  88. <tr>
  89. <td>
  90. <table style="background-color:#FFFFFF; border: dashed 3px #6D7B8D; padding: 5px;width: 100%;table-layout:fixed;" cellpadding="2"
  91. cellspacing="2">
  92. <tr style="height: 30px; color:#123455 ;border: solid 1px #659EC7;">
  93. <td width="40px"> </td>
  94. <td width="60%">
  95. <h1> My Recent Orders Items <strong style="color:#0094ff"> ({{totalItem}})</strong></h1>
  96. </td>
  97. <td align="right">
  98. <button (click)=showItems() style="background-color:#0094ff;color:#FFFFFF;font-size:large;width:300px;height:50px;
  99. border-color:#a2aabe;border-style:dashed;border-width:2px;">
  100. Add More Items
  101. </button>
  102. </td>
  103. </tr>
  104. </table>
  105. </td>
  106. </tr>
  107. <tr>
  108. <td>
  109. <table style="background-color:#FFFFFF; border:solid 2px #6D7B8D;padding: 5px;width: 100%;table-layout:fixed;" cellpadding="2" cellspacing="2">
  110. <tr style="height: 30px; background-color:#336699 ; color:#FFFFFF ;border: solid 1px #659EC7;">
  111. <td width="30" align="center">No</td>
  112. <td width="80" align="center">
  113. <b>Image</b>
  114. </td>
  115. <td width="90" align="center" style="border: solid 1px #FFFFFF; padding: 5px;table-layout:fixed;cursor: pointer;">
  116. <b>Item Code</b>
  117. </td>
  118. <td width="140" align="center" style="border: solid 1px #FFFFFF; padding: 5px;table-layout:fixed;cursor: pointer;">
  119. <b>Item Name</b>
  120. </td>
  121. <td width="160" align="center" style="border: solid 1px #FFFFFF; padding: 5px;table-layout:fixed;cursor: pointer;">
  122. <b>Decription</b>
  123. </td>
  124. <td width="90" align="center" style="border: solid 1px #FFFFFF; padding: 5px;table-layout:fixed;cursor: pointer;">
  125. <b>Price</b>
  126. </td>
  127. <td width="90" align="center" style="border: solid 1px #FFFFFF; padding: 5px;table-layout:fixed;cursor: pointer;">
  128. <b>Quantity</b>
  129. </td>
  130. <td width="90" align="center" style="border: solid 1px #FFFFFF; padding: 5px;table-layout:fixed;cursor: pointer;">
  131. <b>Total Price</b>
  132. </td>
  133. <td></td>
  134. </tr>
  135. <tbody *ngFor="let detail of cartDetails ; let i = index">
  136. <tr>
  137. <td style="border: solid 1px #659EC7; padding: 5px;table-layout:fixed;" align="center">
  138. {{i+1}}
  139. </td>
  140. <td align="center" style="border: solid 1px #659EC7; padding: 5px;table-layout:fixed;">
  141. <span style="color:#9F000F" *ngIf!="getImagename(detail.CImage_Name)">
  142. <img src="{{ImageUrl}}" style="height:56px;width:56px">
  143. </span>
  144. </td>
  145. <td style="border: solid 1px #659EC7; padding: 5px;table-layout:fixed;">
  146. <span style="color:#9F000F">
  147. {{detail.CItem_ID}}
  148. </span>
  149. </td>
  150. <td style="border: solid 1px #659EC7; padding: 5px;table-layout:fixed;">
  151. <span style="color:#9F000F">
  152. {{detail.CItem_Name}}
  153. </span>
  154. </td>
  155. <td style="border: solid 1px #659EC7; padding: 5px;table-layout:fixed;">
  156. <span style="color:#9F000F">
  157. {{detail.CDescription}}
  158. </span>
  159. </td>
  160. <td align="right" style="border: solid 1px #659EC7; padding: 5px;table-layout:fixed;">
  161. <span style="color:#9F000F">
  162. {{detail.CItem_Price | number}}
  163. </span>
  164. </td>
  165. <td style="border: solid 1px #659EC7; padding: 5px;table-layout:fixed;" align="right">
  166. <span style="color:#9F000F">
  167. {{detail.CQty}}
  168. </span>
  169. </td>
  170. <td style="border: solid 1px #659EC7; padding: 5px;table-layout:fixed;" align="right">
  171. <span style="color:#9F000F">
  172. {{detail.CTotalPrice*detail.CQty | number}}
  173. </span>
  174. </td>
  175. <td align="center" style="border: solid 1px #659EC7; padding: 5px;table-layout:fixed;">
  176. <button (click)=removeFromCart(i) style="background-color:#e11919;color:#FFFFFF;font-size:large;width:220px;height:40px;">
  177. Remove Item from Cart
  178. </button>
  179. </td>
  180. </tr>
  181. </tbody>
  182. <tr>
  183. <td colspan="5" height="40" align="right" > <strong>Total </strong></td>
  184. <td align="right" height="40"><strong>Price: {{ totalPrice | number}}</strong></td>
  185. <td align="right" height="40"><strong>Qty : {{ totalQty | number}}</strong></td>
  186. <td align="right" height="40"><strong>Sum: {{ GrandtotalPrice | number}}</strong></td>
  187. <td></td>
  188. </tr>
  189. </table>
  190. </td>
  191. </tr>
  192. </table>
  193. </td>
  194. </tr>
  195. <tr *ngIf="showDetailsTable">
  196. <td>
  197. <table width="100%" style="background-color:#FFFFFF; border: dashed 3px #6D7B8D; padding: 5px;width: 100%;table-layout:fixed;" cellpadding="2"
  198. cellspacing="2">
  199. <tr>
  200. <td>
  201. <table style="background-color:#FFFFFF; border: dashed 3px #6D7B8D; padding: 5px;width: 100%;table-layout:fixed;" cellpadding="2"
  202. cellspacing="2">
  203. <tr style="height: 30px; color:#134018 ;border: solid 1px #659EC7;">
  204. <td width="40px"> </td>
  205. <td width="60%">
  206. <h2> <strong>Item Details</strong></h2>
  207. </td>
  208. <td align="right">
  209. <button (click)=showShoppingItems() style="background-color:#d55500;color:#FFFFFF;font-size:large;width:300px;height:50px;
  210. border-color:#a2aabe;border-style:dashed;border-width:2px;">
  211. Show My Cart Items
  212. </button>
  213. </td>
  214. </tr>
  215. </table>
  216. </td>
  217. </tr>
  218. <tr>
  219. <td>
  220. <table style="background-color:#FFFFFF; border: solid 2px #6D7B8D; padding: 5px;width: 100%;table-layout:fixed;" cellpadding="2" cellspacing="2" *ngIf="ShoppingDetails">
  221. <tr style="height: 30px; background-color:#336699 ; color:#FFFFFF ;border: solid 1px #659EC7;">
  222. <td width="40" align="center">
  223. <b>Image</b>
  224. </td>
  225. <td width="40" align="center" style="border: solid 1px #FFFFFF; padding: 5px;table-layout:fixed;cursor: pointer;">
  226. <b>Item Code</b>
  227. </td>
  228. <td width="120" align="center" style="border: solid 1px #FFFFFF; padding: 5px;table-layout:fixed;cursor: pointer;">
  229. <b>Item Name</b>
  230. </td>
  231. <td width="120" align="center" style="border: solid 1px #FFFFFF; padding: 5px;table-layout:fixed;cursor: pointer;">
  232. <b>Decription</b>
  233. </td>
  234. <td width="40" align="center" style="border: solid 1px #FFFFFF; padding: 5px;table-layout:fixed;cursor: pointer;">
  235. <b>Price</b>
  236. </td>
  237. <td width="90" align="center" style="border: solid 1px #FFFFFF; padding: 5px;table-layout:fixed;cursor: pointer;">
  238. <b>User Name</b>
  239. </td>
  240. </tr>
  241. <tr style="height: 30px; background-color:#336699 ; color:#FFFFFF ;border: solid 1px #659EC7;">
  242. <td width="40" align="center">
  243. Filter By ->
  244. </td>
  245. <td width="200" colspan="5" style="border: solid 1px #FFFFFF; padding: 5px;table-layout:fixed;">
  246. Item Name :
  247. <input type="text" (ngModel)="ItemName" (keyup)="getShoppingDetails(myInput.value)" #myInput style="background-color:#fefcfc;color:#334668;font-size:large;
  248. border-color:#a2aabe;border-style:dashed;border-width:2px;" />
  249. </td>
  250. </tr>
  251. <tbody *ngFor="let detail of ShoppingDetails">
  252. <tr>
  253. <td align="center" style="border: solid 1px #659EC7; padding: 5px;table-layout:fixed;">
  254. <span style="color:#9F000F" *ngIf!="getImagename(detail.image_Name)">
  255. <img src="{{ImageUrl}}" style="height:56px;width:56px" (click)=showToCart(detail.item_ID,detail.item_Name,detail.item_Price,detail.image_Name,detail.description,detail.addedBy)>
  256. </span>
  257. </td>
  258. <td style="border: solid 1px #659EC7; padding: 5px;table-layout:fixed;">
  259. <span style="color:#9F000F">
  260. {{detail.item_ID}}
  261. </span>
  262. </td>
  263. <td style="border: solid 1px #659EC7; padding: 5px;table-layout:fixed;">
  264. <span style="color:#9F000F">
  265. {{detail.item_Name}}
  266. </span>
  267. </td>
  268. <td style="border: solid 1px #659EC7; padding: 5px;table-layout:fixed;">
  269. <span style="color:#9F000F">
  270. {{detail.description}}
  271. </span>
  272. </td>
  273. <td align="right" style="border: solid 1px #659EC7; padding: 5px;table-layout:fixed;">
  274. <span style="color:#9F000F">
  275. {{detail.item_Price}}
  276. </span>
  277. </td>
  278. <td style="border: solid 1px #659EC7; padding: 5px;table-layout:fixed;">
  279. <span style="color:#9F000F">
  280. {{detail.addedBy}}
  281. </span>
  282. </td>
  283. </tr>
  284. </table>
  285. </td>
  286. </tr>
  287. </table>
  288. </td>
  289. </tr>
  290. </table>

Step 7 Adding Students Navigation menu

We can add our newly created Student details menu in existing menu. To add our new Navigation menu, open the “navmenu.component.html” under navmenu menu.Write the below code to add our navigation menu link for students. Here, we have removed the existing Count and Fetch menu.

  1. <li [routerLinkActive]="['link-active']">
  2. <a [routerLink]="['/shopping]">
  3. <span class='glyphicon glyphicon-th-list'></span> Shopping
  4. </a>
  5. </li>

Step 8 App Module

App Module is root for all files and we can find the app.module.ts under ClientApp\ app.

  1. Import our students component
  2. import { shoppingComponent } from './components/shopping/shopping.component';

Next in @NGModule add import { shoppingComponent } from '.

In routing, add your students path. The code will look like this.

  1. import { NgModule } from '@angular/core';
  2. import { FormsModule } from '@angular/forms';
  3. import { BrowserModule } from '@angular/platform-browser';
  4. import { RouterModule } from '@angular/router';
  5. import { UniversalModule } from 'angular2-universal';
  6. import { AppComponent } from './components/app/app.component'
  7. import { NavMenuComponent } from './components/navmenu/navmenu.component';
  8. import { HomeComponent } from './components/home/home.component';
  9. import { FetchDataComponent } from './components/fetchdata/fetchdata.component';
  10. import { CounterComponent } from './components/counter/counter.component';
  11. import { shoppingComponent } from './components/shopping/shopping.component';
  12. @NgModule({
  13. bootstrap: [ AppComponent ],
  14. declarations: [
  15. AppComponent,
  16. NavMenuComponent,
  17. CounterComponent,
  18. FetchDataComponent,
  19. HomeComponent,
  20. shoppingComponent
  21. ],
  22. imports: [
  23. UniversalModule, // Must be first import. This automatically imports BrowserModule, HttpModule, and JsonpModule too.
  24. RouterModule.forRoot([
  25. { path: '', redirectTo: 'home', pathMatch: 'full' },
  26. { path: 'home', component: HomeComponent },
  27. { path: 'counter', component: CounterComponent },
  28. { path: 'fetch-data', component: FetchDataComponent },
  29. { path: 'shopping', component: shoppingComponent },
  30. { path: '**', redirectTo: 'home' }
  31. ])
  32. ]
  33. })
  34. export class AppModule {
  35. }

Step 9 Build and run the application

Build and run the application and you can see that our Students Master/Detail page will be loaded with all Student Master and Detail information.

Note

First, create the Database and Table in your SQL Server. You can run the SQL Script from this article to create ShoppingDB database and ItemDetails Table. Also, don’t forget to change the connection string from “appsettings.json”.