Skills Counter Dynamically in ASP.Net MVC

Introduction
 
This article will explain, how to create counter count of skills dynamically in ASP.Net MVC.  
 
This article requires the following basic understanding
  • Bootstrap 4
  • CSS3
  • JQuery
  • MVC
  • Entity Framework
  • View Model
  • SQL Server 
Step-1 Open your favorite SQL server database, any version.  It doesn’t really matter.  Create tables’ frontend technology and backend technology.
  1. CREATE TABLE [dbo].[FrontendTechnology](    
  2.     [Id] [int] IDENTITY(1,1) NOT NULL,    
  3.     [ProgrammingLanguage] [nvarchar](50) NULL,    
  4.     [Percentage] [intNULL,    
  5.  CONSTRAINT [PK_FrontendTechnology] PRIMARY KEY CLUSTERED     
  6. (    
  7.     [Id] ASC    
  8. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ONON [PRIMARY]    
  9. ON [PRIMARY]    
  10.     
  11. GO    
  12.     
  13. CREATE TABLE [dbo].[BackendTechnology](    
  14.     [Id] [int] IDENTITY(1,1) NOT NULL,    
  15.     [ProgrammingLanguage] [nvarchar](50) NULL,    
  16.     [Percentage] [intNULL,    
  17.  CONSTRAINT [PK_BackendTechnology] PRIMARY KEY CLUSTERED     
  18. (    
  19.     [Id] ASC    
  20. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ONON [PRIMARY]    
  21. ON [PRIMARY]    
  22.     
  23. GO    
Step-2 Now, open your favorite visual studio 2017, or any version you wish.
 
 
Step-3 Create an empty project in visual studio and give an appropriate name.  Check MVC checkbox and click on OK.
 
 
 
Step-4 Right-click on the Models folder and add a database model.  Add Entity Framework now.  For that, right-click on Models folder, select Add, then select New Item.
 
 
 
You will get a window; from there, select Data from the left panel and choose ADO.NET Entity Data Model, give it the name SkillModel (this name is not mandatory, you can give any name) and click "Add"
 
 
 
After you click on "Add a window", the wizard will open.  Choose EF Designer, from the database, and click "Next".
 
 
 
After clicking on "Next", a window will appear.  Choose New Connection.  Another window will appear.  Add your server name.  If it is local, then enter a dot (.). Choose your database and click "OK".
 
 
 
The connection will be added.  If you wish, save the connection name, as you want.  You can change the name of your connection below.  It will save the connection in the web config.  Now, click "Next".
 
 
 
After clicking on NEXT, another window will appear.  Choose the database table name, as shown, in the below screenshot and click "Finish".
 
 
 
Entity Framework gets added and the respective class gets generated under the Models folder.
 
 
 
Step 5 Right-click on Controllers folder and add a controller.
 
 
 
A window will appear.  Choose MVC5 Controller-Empty and click "Add".
 
 
 
After clicking on "Add", another window will appear with DefaultController.  Change the name to HomeController and click "Add".  The HomeController will be added under the Controllers folder.  Don’t change the Controller suffix for all controllers, change only the highlight, and instead of Default, just change Home.
 
 
 
Step-6 Right click on project “Add Folder” and name it ViewModel.  Now, right click on this folder and “Add Class”, with name SkillViewModel, and write following code.
  1. using MvcSkillsCounter_Demo.Models;  
  2. using System.Collections.Generic;  
  3.   
  4. namespace MvcSkillsCounter_Demo.ViewModel  
  5. {  
  6.     public class SkillViewModel  
  7.     {  
  8.         public IEnumerable<FrontendTechnology> FrontendTechnologies { get; set; }  
  9.         public IEnumerable<BackendTechnology> BackendTechnologies { get; set; }   
  10.     }  
  11. }  
Controller Class Code
  1. using MvcSkillsCounter_Demo.Models;  
  2. using MvcSkillsCounter_Demo.ViewModel;  
  3. using System.Linq;  
  4. using System.Web.Mvc;  
  5.   
  6. namespace MvcSkillsCounter_Demo.Controllers  
  7. {  
  8.     public class HomeController : Controller  
  9.     {  
  10.         private readonly MvcDemoContext db = new MvcDemoContext();  
  11.   
  12.         public ActionResult Index()  
  13.         {  
  14.             var skillsCounter = new SkillViewModel  
  15.             {  
  16.                 FrontendTechnologies = db.FrontendTechnologies.ToList(),  
  17.                 BackendTechnologies = db.BackendTechnologies.ToList()  
  18.             };  
  19.             return View(skillsCounter);  
  20.         }  
  21.     }  
  22. }  
Step-7 Right click on the Index action method in controller class “Add View”
 
 
 
 
 
Index View Code
  1. @model MvcSkillsCounter_Demo.ViewModel.SkillViewModel  
  2.   
  3. @{  
  4.     ViewBag.Title = "Index";  
  5. }  
  6.   
  7. <div class="container py-4">  
  8.     <h3 class="technology">Frontend Technologies</h3>  
  9.     <div class="row">  
  10.         @foreach (var frontend in Model.FrontendTechnologies)  
  11.         {  
  12.             <div class="col-md-2">  
  13.                 <div class="skill">  
  14.                     <span class="title">@frontend.ProgrammingLanguage</span>  
  15.                     <p class="counter"><span class="skill-count">@frontend.Percentage</span><span>%</span></p>    
  16.                 </div>  
  17.             </div>  
  18.         }  
  19.     </div>  
  20.     <h3 class="technology">Backend Technologies</h3>  
  21.     <div class="row">  
  22.         @foreach (var backend in Model.BackendTechnologies)  
  23.         {  
  24.         <div class="col-md-2">  
  25.             <div class="skill">  
  26.                 <span class="title">@backend.ProgrammingLanguage</span>  
  27.                 <p class="counter"><span class="skill-count">@backend.Percentage</span><span>%</span></p>  
  28.             </div>  
  29.         </div>  
  30.         }  
  31.     </div>  
  32. </div>  
  33. <script src="~/Scripts/jquery-3.4.1.min.js"></script>  
  34. <script type="text/javascript">  
  35.     $(document).ready(function () {  
  36.         $('.skill-count').each(function () {  
  37.             $(this).prop('Counter', 0).animate({  
  38.                 Counter: $(this).text()  
  39.             }, {  
  40.                     duration: 5000,  
  41.                     easing: 'swing',  
  42.                     step: function (now) {  
  43.                         $(this).text(Math.ceil(now));  
  44.                     }  
  45.                 });  
  46.         });  
  47.     });  
  48. </script>  
Step-8 Install latest version of bootstrap and JQuery from NuGet package manager, under tool in visual studio.
 
Step-9 Write following CSS, if you wish a similar design in Site.css file under content folder.
  1. .technology {    
  2.     font-size18px;    
  3.     color: darkblue;    
  4.     text-transformuppercase;    
  5.     margin15px 0 15px 0;    
  6. }    
  7. .skill {    
  8.     text-aligncenter;    
  9.     border1px solid var(--gray);    
  10.     border-radius: 5px;    
  11.     box-shadow: 2px 2px 4px var(--gray);    
  12.     padding10px;    
  13. }    
  14. .title {    
  15.     font-size16px;    
  16.     font-weight700;    
  17.     color: var(--pink);    
  18.     text-aligncenter;    
  19. }    
  20. .counter {    
  21.     width50px;    
  22.     height50px;    
  23.     line-height50px;    
  24.     text-aligncenter;    
  25.     border1px solid var(--pink);    
  26.     border-radius: 50%;    
  27.     background: var(--pink);    
  28.     colorwhite;    
  29.     vertical-alignmiddle;    
  30.     font-size15px;    
  31.     font-weight700;    
  32.     margin:auto;    
  33. }  
Step-10 Build your project and Run by pressing Ctrl+F5
 
 


Similar Articles