Progress Bar in ASP.NET MVC

Introduction 

 
In this article, we will learn how to create a dynamic progress bar using Bootstrap, Jquery, and JavaScript in ASP.NET MVC.
 
Basically, a Progress Bar is used to show the progress of any task which is being carried out. Progress bars are usually used to display the download and upload status. Moreover, we can say that Progress Bars can be used to depict the status of anything that is in progress. Since we are integrating this is MVC using C#, we are using Bootstrap, JS, and Jquery to implement this in the webpage So let's see how this can be implemented in MVC in the below article.
 
Steps 
 
Create an application in ASP.NET MVC web application in Visual Studio
 
Go to File menu>New>Project
 
Select ASP.NET Web Application (.NET Framework) and change the application name: e.g.,ProgressWebsite, and then click OK
 
Choose MVC>Now, the MVC web application project is created with the default ASP.NET MVC template.
 
If you have an existing database then:
 
Create a Database Model of your existing Database using the Entity Framework
 
The database used in the below example is Db1
 
Create a table: e.g. EMPLOYEE
 
The structure of the table should be as shown below:
  • Name (varchar(50))
  • Father’s Name (varchar(50))
  • Gender (varchar(10))
  • Address (varchar(100))
  • Phone (varchar(12))
  • Mobile No (varchar(10))
If you do not have an existing database, then add a new model in your project and create a new class EMPLOYEE with the above structure.
 
In this example, we are representing the EMPLOYEE data in a tabular form on a web page where we are loading 4000 records to be displayed, which will show the current loading progress using the Progress Bar.
 
Now, add a new controller to your project.
 
Eg: ProgressController
 
Create a Global instance of the Entity in the controller created above:
  1. Db1 db = new Db1();  
Creating non-parameterized Get Action method namely (Emp_Display) in the controller for the view on which records and the progress bar will be displayed.
  1. public ActionResult Emp_Display() {  
  2.     var rec = db.EMPLOYEE.ToList();  
  3.     return View(rec);  
  4. }  
On the View of the ActionMethod (Emp_Display()) we will create a table to display headers of the column, as shown below:
  1. <table class="table">  
  2.     <tr>  
  3.         <th>  
  4.             @Html.DisplayNameFor(model => model. Name)  
  5.          </th>  
  6.         <th>  
  7.             @Html.DisplayNameFor(model => model. Father’sName)  
  8.          </th>  
  9.         <th>  
  10.             @Html.DisplayNameFor(model => model. Gender)  
  11.          </th>  
  12.         <th>  
  13.             @Html.DisplayNameFor(model => model. Address)  
  14.          </th>  
  15.         <th>  
  16.             @Html.DisplayNameFor(model => model.Phone)  
  17.          </th>  
  18.         <th>  
  19.             @Html.DisplayNameFor(model => model.Mobileno)  
  20.          </th>  
  21.     </tr>  
  22.     <tbody class="tbody" id="tbody"></tbody>  
  23. </table>  
Here, we will leave table body empty as it will display records that will be fetched using JavaScript. Then we will provide it a unique ID to identify it when rendering our data.
The classes given to the table above are some of the many bootstrap classes.
 
In the controller class “ProgressController”, we will Create a JSON web method which will be called with the Ajax Jquery to fetch the data and render in the view.
  1. [System.Web.Services.WebMethod]  
  2. public JsonResult GetText() {  
  3.     Db1 db = new Db1();  
  4.     var rec = db.EMPLOYEE.ToList();  
  5.     var serializer = new JavaScriptSerializer() {  
  6.         MaxJsonLength = 999999999  
  7.     };  
  8.     // Perform your serialization  
  9.     serializer.Serialize(rec);  
  10.     return Json(rec, JsonRequestBehavior.AllowGet);  
  11. }  
In the above code, we have attached the WebMethod attribute to a Public method which indicates that we want the method exposed as part of the XML Web service. Adding this attribute to a method within an XML Web service created using ASP.NET makes the method callable from remote Web clients. This class cannot be inherited.
 
Also, we have used serializer. Serialize as JsonSerializer converts .NET objects into their JSON equivalent and back again by mapping the .NET object property names to the JSON property names and copies the values for you.
 
We have implemented the above GetText() Json Action Method that returns the data as JsonResult which is called through Jquery on the View.
 
After creating Json WebMethod on the controller, we will create JS function using AJAX to fetch the data from the WebMethod written above.
 
Below is the code that will call the WebMethod using Ajax.
 
Note
Write the below code on the View in a script Tag.
  1. $(document).ready(function() {  
  2.             var count = 0;  
  3.             var html = '';  
  4.             $(".progress-bar").attr("aria-valuenow""0"); //setting initial value of progressbar as ‘0’  
  5.             $.ajax({  
  6.                 type: "POST",  
  7.                 url: "../ Progress/GetText",  
  8.                 data: "{}",  
  9.                 contentType: "application/json; charset=utf-8",  
  10.                 dataType: "json",  
  11.                 async: false,  
  12.                 success: function(result) {  
  13.                     $.each(result, function(key, item) {  
  14.                         $('.table').hide();  
  15.                         html += '<tr>';  
  16.                         html += '<td>' + item.Name + '</td>';  
  17.                         html += '<td>' + item.Father’ sName + '</td>';  
  18.                         html += '<td>' + item.Gender + '</td>';  
  19.                         html += '<td>' + item.Address + '</td>';  
  20.                         html += '<td>' + item.Phone + '</td>';  
  21.                         html += '<td>' + item.Mobileno + '</td>';  
  22.                         html += '</tr>';  
  23.                         var myVar = setTimeout(updateProgress, 1, ++count, result.length, html);  
  24.                         html = "";  
  25.                     });  
  26.                     $('.table').show();  
  27.                 },  
  28.                 error: function(errormessage) {  
  29.                     $("#h3").text(errormessage.responseText);  
  30.                     return false;  
  31.                 }  
  32.             });  
To display the progress bar, we have to write the below code on the same view page where we want our progress bar and script on the same page as well.
  1. <div class="progress">  
  2.    <div class="progress-bar progress-bar-success progress-bar-striped active" role="progressbar" aria-valuenow="" aria-valuemin="0" aria-valuemax="100" id="lblStatus">  
  3. </div>  
  4. </div>  
The below JavaScript function is used to update the progress bar to display the loading of data in percentage:
  1. function updateProgress(count, max, html) {  
  2.     $("#h3").text(count + " Records loaded successfully");  
  3.     var lblStatus = document.getElementById("lblStatus");  
  4.     lblStatus.style.width = count1 + "%";  
  5.     $("#lblStatus").text(count1 + '% Complete');  
  6.     $(".tbody").append(html);  
  7. }  
Now, below are the complete code pages explained above.
 
Controller
  1. public class ProgressController: Controller {  
  2.     // GET: Progress  
  3.     Db1 db = new Db1();  
  4.     public ActionResult Index() {  
  5.         return View();  
  6.     }  
  7.     public ActionResult Emp_Display() {  
  8.             return View(db.EMPLOYEE.ToList());  
  9.         }  
  10.         [System.Web.Services.WebMethod]  
  11.     public JsonResult GetText() {  
  12.         Db1 db = new Db1();  
  13.         var rec = db.EMPLOYEE.ToList();  
  14.         var serializer = new JavaScriptSerializer() {  
  15.             MaxJsonLength = 999999999  
  16.         };  
  17.         // Perform your serialization  
  18.         serializer.Serialize(rec);  
  19.         return Json(rec, JsonRequestBehavior.AllowGet);  
  20.     }  
  21. }  
View
  1. @model IEnumerable  
  2. <SignalR.EMPLOYEE>  
  3.     <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>  
  4.     <div class="progress">  
  5.         <div class="progress-bar progress-bar-success progress-bar-striped active" role="progressbar" aria-valuenow="" aria-valuemin="0" aria-valuemax="100" id="lblStatus"></div>  
  6.     </div>  
  7.     <h3 id="h3"></h3>  
  8.     <table class="table">  
  9.         <tr>  
  10.             <th>  
  11.                @Html.DisplayNameFor(model => model. Name)  
  12.             </th>  
  13.             <th>  
  14.                @Html.DisplayNameFor(model => model. Father’sName)  
  15.             </th>  
  16.             <th>  
  17.                @Html.DisplayNameFor(model => model. Gender)  
  18.             </th>  
  19.             <th>  
  20.                @Html.DisplayNameFor(model => model. Address)  
  21.             </th>  
  22.             <th>  
  23.                @Html.DisplayNameFor(model => model.Phone)  
  24.             </th>  
  25.             <th>  
  26.                @Html.DisplayNameFor(model => model.Mobileno)  
  27.             </th>  
  28.         </tr>  
  29.         <tbody class="tbody" id="tbody"></tbody>  
  30.     </table>  
  31.     <script>  
  32. $(document).ready(function() {  
  33.     var count = 0;  
  34.     var html = ''”;  
  35.     $(".progress-bar").attr("aria-valuenow""0");  
  36.     $.ajax({  
  37.         type: "POST",  
  38.         url: "../Progress/GetText",  
  39.         data: "{}",  
  40.         contentType: "application/json; charset=utf-8",  
  41.         dataType: "json",  
  42.         async: false,  
  43.         success: function(result) {  
  44.             $.each(result, function(key, item) {  
  45.                 $('.table').hide();  
  46.                 html += '<tr>';  
  47.                 html += '<td>' + item.Name + '</td>';  
  48.                 html += '<td>' + item.Father’ sName + '</td>';  
  49.                 html += '<td>' + item.Gender + '</td>';  
  50.                 html += '<td>' + item.Address + '</td>';  
  51.                 html += '<td>' + item.Phone + '</td>';  
  52.                 html += '<td>' + item.Mobileno + '</td>';  
  53.                 html += '</tr>';  
  54.                 var myVar = setTimeout(updateProgress, 10, ++count, result.length, html);  
  55.                 html = "";  
  56.             });  
  57.             $('.table').show();  
  58.         },  
  59.         error: function(errormessage) {  
  60.             $("#h3").text(errormessage.responseText);  
  61.             return false;  
  62.         }  
  63.     });  
  64.     var count = 1;  
  65.   
  66.     function updateProgress(count, max, html) {  
  67.         $("#h3").text(count + " Records loaded successfully");  
  68.         if (count <= max) {  
  69.             count1 = parseInt(count / (max / 100));  
  70.             var lblStatus = document.getElementById("lblStatus");  
  71.             lblStatus.style.width = count1 + "%";  
  72.             $("#lblStatus").text(count1 + '% Complete');  
  73.             $(".tbody").append(html);  
  74.         }  
  75.     }  
  76. }); < /script>  
In this article,we have used Javascript, which is a light weighted scripting language that helps us make use of HTML and CSS to display the loading of data in a dynamic way using this highly customizable progress bar/loading indicator on top of the webpage.