ASP.NET Core - Using Highcharts With Angular 5

Introduction

In this article, we will create an online poll application using ASP.NET Core, Angular 5, and Entity Framework Core. Since this is the season of IPL in India, we will create an online poll for “Who is going to win IPL 2018?”. The poll results will be displayed as a column chart, that is created using Highcharts.

We will use Visual Studio 2017 and SQL Server 2014.

Take a look at the final application.
 
 
Prerequisites
  • Install .NET Core 2.0.0 or above SDK from here.
  • Install the latest version of Visual Studio 2017 Community Edition from here.
  • Download and install the latest version of Node.js from here.
  • SQL Server 2008 or above.
Source Code

Before proceeding, I would recommend you to get the source code from GitHub

Creating Table

We will store the team data in IplTeams table. Execute the following commands to create the table.

  1. CREATE TABLE IplTeams    
  2. (  
  3. TeamId INTEGER IDENTITY(1,1) PRIMARY KEY,  
  4. TeamName VARCHAR(30) NOT NULL,  
  5. VoteCount INTEGER NOT NULL  
  6. )  
Now, we will put in the team names and initialize the vote count to zero. Execute the following insert statements.
  1. INSERT INTO IplTeams VALUES ('Chennai Super Kings',0)  
  2. INSERT INTO IplTeams VALUES ('Delhi Daredevils',0)  
  3. INSERT INTO IplTeams VALUES ('Kings XI Punjab',0)  
  4. INSERT INTO IplTeams VALUES ('Kolkata Knight Riders',0)  
  5. INSERT INTO IplTeams VALUES ('Mumbai Indians',0)  
  6. INSERT INTO IplTeams VALUES ('Rajasthan Royals',0)  
  7. INSERT INTO IplTeams VALUES ('Royal Challengers Bangalore',0)  
  8. INSERT INTO IplTeams VALUES ('Sunrisers Hyderabad',0)  

Create MVC Web Application

Open Visual Studio and select File >> New >> Project. After selecting the project, a "New Project" dialog will open. Select .NET Core inside Visual C# menu from the left panel.

Then, select “ASP.NET Core Web Application” from available project types. Put the name of the project as IPLPollDemo and press OK.
 
 
 
After clicking on OK, a new dialog will open asking you to select the project template. You can observe two drop-down menus at the top left of the template window. Select “.NET Core” and “ASP.NET Core 2.0” from these dropdowns. Then, select “Angular” template and press OK.
 
 
 
Now, our project is created.
 
Since we are using Highcharts in our application, we need to install packages for it. Open package.json file and put in the following code into it.
  1. {  
  2.   "name": "IPLPollDemo",  
  3.   "private": true,  
  4.   "version": "0.0.0",  
  5.   "scripts": {  
  6.     "test": "karma start ClientApp/test/karma.conf.js"  
  7.   },  
  8.   "devDependencies": {  
  9.     "@angular/animations": "5.2.10",  
  10.     "@angular/common": "5.2.10",  
  11.     "@angular/compiler": "5.2.10",  
  12.     "@angular/compiler-cli": "5.2.10",  
  13.     "@angular/core": "5.2.10",  
  14.     "@angular/forms": "5.2.10",  
  15.     "@angular/http": "5.2.10",  
  16.     "@angular/platform-browser": "5.2.10",  
  17.     "@angular/platform-browser-dynamic": "5.2.10",  
  18.     "@angular/platform-server": "5.2.10",  
  19.     "@angular/router": "5.2.10",  
  20.     "@ngtools/webpack": "6.0.0-rc.10",  
  21.     "@types/chai": "4.1.3",  
  22.     "@types/highcharts": "^5.0.22",  
  23.     "@types/jasmine": "2.8.6",  
  24.     "@types/webpack-env": "1.13.6",  
  25.     "angular2-router-loader": "0.3.5",  
  26.     "angular2-template-loader": "0.6.2",  
  27.     "aspnet-prerendering": "^3.0.1",  
  28.     "aspnet-webpack": "^2.0.1",  
  29.     "awesome-typescript-loader": "5.0.0",  
  30.     "bootstrap": "4.1.1",  
  31.     "chai": "4.1.2",  
  32.     "css": "2.2.1",  
  33.     "css-loader": "0.28.11",  
  34.     "es6-shim": "0.35.3",  
  35.     "event-source-polyfill": "0.0.12",  
  36.     "expose-loader": "0.7.5",  
  37.     "extract-text-webpack-plugin": "3.0.2",  
  38.     "file-loader": "1.1.11",  
  39.     "html-loader": "0.5.5",  
  40.     "isomorphic-fetch": "2.2.1",  
  41.     "jasmine-core": "3.1.0",  
  42.     "jquery": "3.3.1",  
  43.     "json-loader": "0.5.7",  
  44.     "karma": "2.0.2",  
  45.     "karma-chai": "0.1.0",  
  46.     "karma-chrome-launcher": "2.2.0",  
  47.     "karma-cli": "1.0.1",  
  48.     "karma-jasmine": "1.1.1",  
  49.     "karma-webpack": "3.0.0",  
  50.     "preboot": "6.0.0-beta.3",  
  51.     "raw-loader": "0.5.1",  
  52.     "reflect-metadata": "0.1.12",  
  53.     "rxjs": "^6.0.0",  
  54.     "style-loader": "0.21.0",  
  55.     "to-string-loader": "1.1.5",  
  56.     "typescript": "2.8.3",  
  57.     "url-loader": "1.0.1",  
  58.     "webpack": "4.6.0",  
  59.     "webpack-hot-middleware": "2.22.1",  
  60.     "webpack-merge": "4.1.2",  
  61.     "zone.js": "0.8.26"  
  62.   },  
  63.   "dependencies": {  
  64.     "angular-highcharts": "^5.2.12",  
  65.     "highcharts": "^6.1.0"  
  66.   }  
  67. }  
Here, we have added Highcharts dependency in line 22, 64 and 65.

Important Note

If you notice that, the Angular version is 4 in your package.json file then copy the full code as above so as to update your Angular version to 5. If you are already using angular 5 then just copy the lines to include Highcharts dependency.
 
Now, close the Visual Studio instance and navigate to the project folder containing package.json file and open command prompt. Execute “npm install” command to install all the required dependencies. Refer to the image below :
 
 

After the command executes successfully, open your project in Visual Studio. You can observe the folder structure in Solution Explorer as shown in the below image.


Here, we have our Controllers and Views folders. We won’t be touching the Views folders for this tutorial since we will be using Angular to handle the UI. The Controllers folders will contain our Web API controller. The point of interest for us is the ClientApp folder where the client side of our application resides. Inside the ClientApp/app/components folder, we already have few components created which are provided by default with the Angular template in VS 2017. These components won’t affect our application, but for the sake of this tutorial, we will delete fetchdata and counter folders from ClientApp/app/components.

Scaffolding the Model to the Application

We are using Entity Framework core database first approach to create our models. Navigate to Tools >> NuGet Package Manager >> Package Manager Console.
 
We have to install the package for the database provider that we are targeting which is SQL Server in this case. Hence run the following command:
  1. Install-Package Microsoft.EntityFrameworkCore.SqlServer  
Since we are using Entity Framework Tools to create a model from the existing database, we will install the tools package as well. Hence, run the following command:
  1. Install-Package Microsoft.EntityFrameworkCore.Tools  
After you have installed both the packages, we will scaffold our model from the database tables using the following command:
  1. Scaffold-DbContext "Your connection string here" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -Tables IplTeams  
Do not forget to put your own connection string (inside " "). After this command is executed successfully you can observe a Models folder has been created and it contains two class files myTestDBContext.cs and IplTeams.cs. Hence, we have successfully created our Models using EF core database first approach.
 
Now, we will create one more class file to handle database related operations.
 
Right click on Models folder and select Add >> Class. Name your class TeamDataAccessLayer.cs and click Add button. At this point of time, the Models folder will have the following structure. 
 
 
 
Open TeamDataAccessLayer.cs and put the following code to handle database operations.
  1. using Microsoft.EntityFrameworkCore;  
  2. using System;  
  3. using System.Collections.Generic;  
  4. using System.Linq;  
  5. using System.Threading.Tasks;  
  6.   
  7. namespace IPLPollDemo.Models  
  8. {  
  9.     public class TeamDataAccessLayer  
  10.     {  
  11.         myTestDBContext db = new myTestDBContext();  
  12.   
  13.         //To get the list of all teams from database  
  14.         public IEnumerable<IplTeams> GetAllTeams()  
  15.         {  
  16.             try  
  17.             {  
  18.                 return db.IplTeams.ToList();  
  19.             }  
  20.             catch  
  21.             {  
  22.                 throw;  
  23.             }  
  24.         }  
  25.   
  26.         //To update the vote count of a team by one  
  27.         public int RecordVote(IplTeams iplTeam)  
  28.         {  
  29.             try  
  30.             {  
  31.   
  32.                 db.Database.ExecuteSqlCommand("update IplTeams set VoteCount = VoteCount + 1 where TeamID = {0}", parameters: iplTeam.TeamId);  
  33.   
  34.                 return 1;  
  35.             }  
  36.             catch  
  37.             {  
  38.                 throw;  
  39.             }  
  40.         }  
  41.   
  42.         //To get the total votes count   
  43.         public int GetTotalVoteCount()  
  44.         {  
  45.             try  
  46.             {  
  47.                 return db.IplTeams.Sum(t => t.VoteCount);  
  48.             }  
  49.             catch  
  50.             {  
  51.                 throw;  
  52.             }  
  53.         }  
  54.     }  
  55. }   
In this class we have defined three methods.
  1. GetAllTeams – To get the list of all the eight teams from the database.
  2. RecordVote – To update the vote count for each team after the user submits his/her vote.
  3. GetTotalVoteCount – To get the sum of votes of all the teams.
Now, we will proceed to create our Web API Controller.
 
Adding the Web API Controller to the Application

Right click on Controllers folder and select Add >> New Item.

An “Add New Item” dialog box will open. Select ASP.NET from the left panel, then select “Web API Controller Class” from templates panel and put the name as TeamController.cs. Click Add.
 
 
 
This will create our Web API TeamController class. We will put all our business logic in this controller. We will call the methods of TeamDataAccessLayer to fetch data and pass on the data to the Angular frontend.
 
Open TeamController.cs file and put the following code into it.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Threading.Tasks;  
  5. using IPLPollDemo.Models;  
  6. using Microsoft.AspNetCore.Mvc;  
  7.   
  8. namespace IPLPollDemo.Controllers  
  9. {  
  10.     [Route("api/Team")]  
  11.     public class TeamController : Controller  
  12.     {  
  13.         TeamDataAccessLayer objTeam = new TeamDataAccessLayer();  
  14.   
  15.         [HttpGet]  
  16.         [Route("GetTeamList")]  
  17.         public IEnumerable<IplTeams> GetTeamList()  
  18.         {  
  19.             return objTeam.GetAllTeams();  
  20.         }  
  21.   
  22.         [HttpGet]  
  23.         [Route("TotalVotes")]  
  24.         public int TotalVotes()  
  25.         {  
  26.             return objTeam.GetTotalVoteCount();  
  27.         }  
  28.   
  29.         [HttpPut]  
  30.         [Route("UpdateVoteCount")]  
  31.         public int UpdateVoteCount([FromBody] IplTeams team)  
  32.         {  
  33.             return objTeam.RecordVote(team);  
  34.         }  
  35.     }  
  36. }  

Create the Angular Service

We will create an Angular service that will convert the Web API response to JSON and pass it to our component. Right click on ClientApp/app folder and then Add >> New Folder and name the folder as Services.
 
Right click on Sevices folder and select Add >> New Item. An “Add New Item” dialog box will open. Select Scripts from the left panel, then select “TypeScript File” from templates panel, and put the name as teamservice.service.ts. Click Add.
 
 
 
Open teamservice.service.ts file and put the following code into it.
  1. import { Injectable, Inject } from '@angular/core';  
  2. import { Http, Response } from '@angular/http';  
  3. import { Observable } from 'rxjs/Observable';  
  4. import { Router } from '@angular/router';  
  5. import 'rxjs/add/operator/map';  
  6. import 'rxjs/add/operator/catch';  
  7. import 'rxjs/add/observable/throw';  
  8.   
  9.   
  10. @Injectable()  
  11. export class TeamService {  
  12.     myAppUrl: string = "";  
  13.   
  14.     constructor(private _http: Http, @Inject('BASE_URL') baseUrl: string) {  
  15.         this.myAppUrl = baseUrl;  
  16.     }  
  17.   
  18.     getTeams() {  
  19.         return this._http.get(this.myAppUrl + 'api/Team/GetTeamList')  
  20.             .map((response: Response) => response.json())  
  21.             .catch(this.errorHandler);  
  22.     }  
  23.   
  24.     getTotalVotes() {  
  25.         return this._http.get(this.myAppUrl + 'api/Team/TotalVotes')  
  26.             .map((response: Response) => response.json())  
  27.             .catch(this.errorHandler);  
  28.     }  
  29.   
  30.     saveVotes(team) {  
  31.         return this._http.put(this.myAppUrl + 'api/Team/UpdateVoteCount', team)  
  32.             .map((response: Response) => response.json())  
  33.             .catch(this.errorHandler);  
  34.     }  
  35.   
  36.     errorHandler(error: Response) {  
  37.         console.log(error);  
  38.         return Observable.throw(error);  
  39.     }  
  40. }  
In the constructor, we are injecting the HTTP service and Base URL of the application to enable web API calls. After that, we have defined three functions to call our Web API and convert the result to JSON format. We will call these functions from our components. 
 
At this point in time, you might get an error “Parameter 'employee' implicitly has an 'any' type” in empservice.service.ts file. If you encounter this issue, then add the following line inside tsconfig.json file.
 
"noImplicitAny": false
 
 
 
Now, we will proceed to create our components.

Creating Angular Components

We will be adding two Angular components to our application,
  1. Poll component – to display the team names and a corresponding button to vote for the team.
  2. Result component – to display the poll results.

Right click on ClientApp/app/components folder and select Add >> New Folder and name the folder as Poll.

Right click on Poll folder and select Add >> New Item. An “Add New Item” dialog box will open. Select Scripts from the left panel, then select “TypeScript File” from templates panel, and put the name as IPLPoll.component.tsClick Add. This will add a typescript file inside poll folder.
 
 
 
Right click on Poll folder and select Add >> New Item. An “Add New Item” dialog box will open. Select ASP.NET Core from the left panel, then select “HTML Page” from templates panel, and put the name as IPLPoll.component.html. Click Add. This will add an HTML file inside Poll folder.
 
 
 
Similarly create a Results folder inside ClientApp/app/components folder and add PollResult.component.ts typescript file and PollResult.component.html HTML file to it.
 
Now, our ClientApp/app will look like the image below.
 
 
Open IPLPoll.component.ts file and put the following code into it.
  1. import { Component, OnInit } from '@angular/core';  
  2. import { Http, Headers } from '@angular/http';  
  3. import { PercentPipe } from '@angular/common';  
  4. import { Router, ActivatedRoute } from '@angular/router';  
  5. import { TeamService } from '../../services/teamservice.service'  
  6.   
  7. @Component({  
  8.     templateUrl: './IPLPoll.component.html',  
  9. })  
  10.   
  11. export class IPLPoll {  
  12.   
  13.     public teamList: TeamData[];  
  14.   
  15.     constructor(public http: Http, private _teamService: TeamService, private _router: Router) {  
  16.         this.getTeamList();  
  17.     }  
  18.   
  19.     getTeamList() {  
  20.         this._teamService.getTeams().subscribe(  
  21.             data => this.teamList = data  
  22.         )  
  23.     }  
  24.   
  25.     save(team) {  
  26.   
  27.         this._teamService.saveVotes(team)  
  28.             .subscribe((data) => {  
  29.                 this._router.navigate(['/results']);  
  30.             })  
  31.     }  
  32. }  
  33. export class TeamData {  
  34.     teamId: number;  
  35.     teamName: string;  
  36.     voteCount: number;  
  37.     voteShare: number;  
  38. }  
We have created a class TeamData to hold the details of each team such as teamId, teamName, voteCount and voteShare. Inside our component class IPLPoll , we have created an array variable teamList of type TeamData. The getTeamList() method will call the getTeams function of our service TeamService, to get the list of teams from the database and assign it to the teamList variable. The getTeamList method is called inside the constructor so that the team data will be displayed as the page loads.

The save method will be invoked when the user votes for his favorite team. This will call saveVotes function of our service to update the vote count of that particular team. The user will be then redirected to PollResults component to view the poll results.
 
Open IPLPoll.component.html file and put the following code into it.
  1. <h1>Who Will Win IPL 2018 ?</h1>  
  2.   
  3. <h3>Vote for your favourite team !!! </h3>  
  4. <hr />  
  5.   
  6. <p *ngIf="!teamList"><em>Loading...</em></p>  
  7.   
  8. <table class='table' *ngIf="teamList">  
  9.     <thead>  
  10.         <tr>  
  11.             <th>Team Name</th>  
  12.         </tr>  
  13.     </thead>  
  14.     <tbody>  
  15.         <tr *ngFor="let team of teamList">  
  16.             <td>{{ team.teamName }}</td>  
  17.             <td>  
  18.                 <button (click)="save(team)" class="btn btn-primary"> Vote <i class="glyphicon glyphicon-thumbs-up"></i></button>  
  19.             </td>  
  20.         </tr>  
  21.     </tbody>  
  22. </table>  
This html page will display the list of teams along with a Vote button besides each team. When the user clicks on any of the vote buttons, it will update the vote count and redirects the user to the PollResults page.
 
Now open PollResults.component.ts file and put the following code into it,
  1. import { Component, OnInit } from '@angular/core';  
  2. import { Http, Headers } from '@angular/http';  
  3. import { PercentPipe } from '@angular/common';  
  4. import { Router, ActivatedRoute } from '@angular/router';  
  5. import { TeamData } from '../poll/IPLPoll.component';  
  6. import { TeamService } from '../../services/teamservice.service';  
  7.   
  8. import { Observable } from 'rxjs/Observable';  
  9. import 'rxjs/add/observable/zip';  
  10.   
  11. import { Chart } from 'angular-highcharts';  
  12.   
  13. @Component({  
  14.     templateUrl: './PollResult.component.html',  
  15. })  
  16.   
  17. export class PollResult {  
  18.   
  19.     public chart: any;  
  20.     public totalVotes: number;  
  21.     public resultList: TeamData[];  
  22.   
  23.     constructor(public http: Http, private _teamService: TeamService) {  
  24.   
  25.         Observable.zip(this._teamService.getTotalVotes(), this._teamService.getTeams())  
  26.             .subscribe(([totalVoteCount, teamListData]) => {  
  27.                 this.totalVotes = totalVoteCount;  
  28.                 this.resultList = teamListData;  
  29.   
  30.                 for (let i = 0; i < teamListData.length; i++) {  
  31.                     teamListData[i].voteShare = (((teamListData[i].voteCount) / this.totalVotes) * 100);  
  32.                 }  
  33.   
  34.                 this.createCharts();  
  35.             });  
  36.     }  
  37.   
  38.     createCharts() {  
  39.         this.chart = new Chart({  
  40.             chart: {  
  41.                 type: 'column'  
  42.             },  
  43.             title: {  
  44.                 text: 'Vote share for each team'  
  45.             },  
  46.             xAxis: {  
  47.                 type: 'category',  
  48.                 labels: {  
  49.                     rotation: -45,  
  50.                     style: {  
  51.                         fontSize: '13px',  
  52.                         fontFamily: 'Verdana, sans-serif'  
  53.                     }  
  54.                 }  
  55.             },  
  56.             yAxis: {  
  57.                 min: 0,  
  58.                 title: {  
  59.                     text: 'Percentage of Votes'  
  60.                 }  
  61.             },  
  62.             legend: {  
  63.                 enabled: false  
  64.             },  
  65.             tooltip: {  
  66.                 pointFormat: 'Vote: <b>{point.y:.2f} %</b>'  
  67.             },  
  68.   
  69.             series: [{  
  70.                 type: 'column',  
  71.                 data: [  
  72.                     { name: this.resultList[0].teamName, y: this.resultList[0].voteShare, color: 'rgba(253, 185, 19, 0.85)' },  
  73.                     { name: this.resultList[1].teamName, y: this.resultList[1].voteShare, color: 'rgba(0, 76, 147, 0.85)' },  
  74.                     { name: this.resultList[2].teamName, y: this.resultList[2].voteShare, color: 'rgba(170, 69, 69, 0.85)' },  
  75.                     { name: this.resultList[3].teamName, y: this.resultList[3].voteShare, color: 'rgba(112, 69, 143, 0.85)' },  
  76.                     { name: this.resultList[4].teamName, y: this.resultList[4].voteShare, color: 'rgba(0, 93, 160, 0.85)' },  
  77.                     { name: this.resultList[5].teamName, y: this.resultList[5].voteShare, color: 'rgba(45, 77, 157, 0.85)' },  
  78.                     { name: this.resultList[6].teamName, y: this.resultList[6].voteShare, color: 'rgba(0, 0, 0, 0.85)' },  
  79.                     { name: this.resultList[7].teamName, y: this.resultList[7].voteShare, color: 'rgba(251, 100, 62, 0.85)' }  
  80.                 ],  
  81.             }]  
  82.   
  83.         });  
  84.   
  85.     }  
  86. }   

We are fetching the updated list of team data from the database and also the total count of votes for all the teams. We will then calculate the vote share of each team and then invoke the createCharts() method to create the chart for the poll result. The percentage of vote share for each team is calculated by dividing the vote obtained by each team with the total number of votes. We are doing all these operations in our constructor to display the result as the page loads.

The createCharts() method will create the column chart with the help of Highcharts library. The percentage of votes is selected as Y-axis and the team name is selected as X-axis. To make the things interesting we are setting the color of each column as the corresponding team jersey color. 
 
Open PollResults.component.html file and put the following code into it,
  1. <h2>Your vote has been registered successfully !!! </h2>  
  2.   
  3. <h3>Here are voting results </h3>  
  4. <hr />  
  5.   
  6. <p><b>Total votes </b> : {{totalVotes}}</p>  
  7.   
  8. <div [chart]="chart"></div>  
This HTML page is simple. We are displaying the voting results as a column chart. Just above the chart, we are also displaying the total number of votes.
 
Defining route and navigation menu for our Application

Open /app/app.shared.module.ts file and put the following code into it.
  1. import { NgModule } from '@angular/core';  
  2. import { CommonModule } from '@angular/common';  
  3. import { FormsModule } from '@angular/forms';  
  4. import { HttpModule } from '@angular/http';  
  5. import { RouterModule } from '@angular/router';  
  6. import { ChartModule } from 'angular-highcharts';  
  7.   
  8. import { TeamService } from './services/teamservice.service'  
  9. import { AppComponent } from './components/app/app.component';  
  10. import { NavMenuComponent } from './components/navmenu/navmenu.component';  
  11. import { HomeComponent } from './components/home/home.component';  
  12. import { IPLPoll } from './components/Poll/IPLPoll.component';  
  13. import { PollResult } from './components/Results/PollResult.component';  
  14.   
  15. @NgModule({  
  16.     declarations: [  
  17.         AppComponent,  
  18.         NavMenuComponent,  
  19.         HomeComponent,  
  20.         IPLPoll,  
  21.         PollResult  
  22.     ],  
  23.     imports: [  
  24.         CommonModule,  
  25.         HttpModule,  
  26.         FormsModule,  
  27.         ChartModule,  
  28.         RouterModule.forRoot([  
  29.             { path: '', redirectTo: 'home', pathMatch: 'full' },  
  30.             { path: 'home', component: HomeComponent },  
  31.             { path: 'poll', component: IPLPoll },  
  32.             { path: 'results', component: PollResult },  
  33.             { path: '**', redirectTo: 'home' }  
  34.         ])  
  35.     ],  
  36.     providers: [TeamService]  
  37. })  
  38. export class AppModuleShared {  
  39. }  
Here we have also imported all our components and defined the route for our application as below,
  • home - which will redirect to Home component
  • poll – redirects to IPLPoll component
  • results – redirects to PollResults component 
One last thing is to define navigation menu for our application. Open /app/components/navmenu/navmenu.component.html file and put the following code to it.
  1. <div class='main-nav'>  
  2.     <div class='navbar navbar-inverse'>  
  3.         <div class='navbar-header'>  
  4.             <button type='button' class='navbar-toggle' data-toggle='collapse' data-target='.navbar-collapse'>  
  5.                 <span class='sr-only'>Toggle navigation</span>  
  6.                 <span class='icon-bar'></span>  
  7.                 <span class='icon-bar'></span>  
  8.                 <span class='icon-bar'></span>  
  9.             </button>  
  10.             <a class='navbar-brand' [routerLink]="['/home']">IPLPollDemo</a>  
  11.         </div>  
  12.         <div class='clearfix'></div>  
  13.         <div class='navbar-collapse collapse'>  
  14.             <ul class='nav navbar-nav'>  
  15.                 <li [routerLinkActive]="['link-active']">  
  16.                     <a [routerLink]="['/home']">  
  17.                         <span class='glyphicon glyphicon-home'></span> Home  
  18.                     </a>  
  19.                 </li>  
  20.                 <li [routerLinkActive]="['link-active']">  
  21.                     <a [routerLink]="['/poll']">  
  22.                         <span class='glyphicon glyphicon-th-list'></span> Ipl Poll  
  23.                     </a>  
  24.                 </li>  
  25.             </ul>  
  26.         </div>  
  27.     </div>  
  28. </div>  
And that’s it. We have created out IPL Poll application using angular 5 and Entity Framework core.
 
Execution Demo
 
Press F5 to launch the application.

A web page will open as shown in the image below. Notice the URL showing route for our home component. And navigation menu on the left showing navigation link for Ipl Poll page.
 
 
 
Click on IPL Poll in the navigaton menu. It will redirect to Poll component showing all the team names along with a vote button beside them. Notice the URL has "/Poll" in it.
 
 
 
Click on the vote button to vote for your favorite team. You will be redirected to the results page showing the poll results as a column chart.
 
 
Since this is the first vote, it is showing 100% for one team and 0% for others. After submitting a few votes for all the teams, we will get the voting results chart as shown below.
 
 
 
Conclusion
 
We have created an online poll using ASP.NET Core, Angular 5 and Entity Framework core database first approach with the help of Visual Studio 2017 and SQL Server 2014. We also created a column chart using Highcharts to display the poll results.
 
Get the source code from Github and play around. Do not forget to put your own connection string before executing the code. 
 
See Also