Sort, Page, & Export Data Of HTML Table Using jQuery Datatable In .NET Core MVC

Introduction

 
Here, I introduce you to jQuery.datatable features in our web application in .NET Core MVC Project.

The Model-View-Controller (MVC) architectural pattern separates an application into three main groups of components: Models, Views, and Controllers. This pattern helps to achieve separation of concerns. Using this pattern, user requests are routed to a Controller which is responsible for working with the Model to perform user actions and/or retrieve results of queries. The Controller chooses the View to display to the user, and provides it with any Model data it requires.
 
Jquery.datatables helps us to show the HTML table in a responsive and interactive way by adding Paging, Sorting, Export Events, and many more features and functionality on the client-side.
 
In your existing .NET Core MVC project solution, you need to add client-side library packages for jquery.datatables, which you can also download directly from here.
 
With Datatables, you can alter the ordering characteristics of the table at initialization time. Using the order initialization parameter, you can set the table to display the data in the exact order that you want. 
 
Datatables can split the rows in tables into individual pages, which is an efficient method of showing a large number of records in a small space. The end-user is provided with controls to request the display of different data as they navigate through the data. This feature is enabled by default, but if you wish to disable it, you may do so with this parameter:
 
 
Here, I am using bundleconfig.json for bundling and minification where we register all required files as a single output file.
 
Before adding bundleconfig.json file to your project's root directory, add an extension of Bundler and Minifier to your project.
 
or...
 
You can also directly refer the all JS and CSS file directly to your Page or _Layout.cshtml.
 
bundleconfig.json
  1. [{  
  2.     "outputFileName""wwwroot/js/site.min.js",  
  3.     "inputFiles": ["wwwroot/lib/jquery/dist/jquery.js""wwwroot/datatables/js/jquery.dataTables.js""wwwroot/datatables/js/dataTables.bootstrap.js""wwwroot/datatables/js/dataTables.bootstrap4.js""wwwroot/datatables/js/dataTables.dataTables.js""wwwroot/datatables/js/dataTables.foundation.js""wwwroot/datatables/js/dataTables.jqueryui.js""wwwroot/datatables/js/dataTables.material.js""wwwroot/datatables/js/dataTables.semanticui.js""wwwroot/datatables/js/dataTables.uikit.js""wwwroot/datatables/js/datatables-buttons/js/dataTables.buttons.js""wwwroot/datatables/js/datatables-buttons/js/buttons.colVis.js""wwwroot/datatables/js/datatables-buttons/js/buttons.flash.js""wwwroot/datatables/js/datatables-buttons/js/buttons.html5.js""wwwroot/datatables/js/datatables-buttons/js/buttons.print.js"],  
  4.     "minify": {  
  5.         "enabled"true,  
  6.         "renameLocals"true  
  7.     }  
  8. }, {  
  9.     "outputFileName""wwwroot/js/site.min.css",  
  10.     "inputFiles": ["wwwroot/datatables/css/jquery.dataTables.css""wwwroot/datatables/css/dataTables.bootstrap.css""wwwroot/datatables/css/dataTables.bootstrap4.css""wwwroot/datatables/css/dataTables.foundation.css""wwwroot/datatables/css/dataTables.jqueryui.css""wwwroot/datatables/css/dataTables.material.css""wwwroot/datatables/css/dataTables.semanticui.css""wwwroot/datatables/css/dataTables.uikit.css""wwwroot/datatables/css/buttons.dataTables.min.css"],  
  11.     "minify": {  
  12.         "enabled"true,  
  13.         "renameLocals"true  
  14.     }  
  15. }]   
 
I am using bundling, so I need to make an output file by running the task runner explorer and right-clicking on the bundleconfig.json file. 
 
 
Add controller and View for getting data from the database and showing it to the view in table format.
 
ExampleController.cs
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Threading.Tasks;  
  5. using Microsoft.AspNetCore.Mvc;  
  6. using Microsoft.AspNetCore.Mvc.Rendering;  
  7. using Microsoft.EntityFrameworkCore;  
  8. using TestDemo.DBLayer;  
  9. using TestDemo.Models;  
  10.   
  11. namespace TestDemo.Controllers  
  12. {  
  13.     public class ExampleController : Controller  
  14.     {  
  15.         private readonly DBAccessContext _context;  
  16.   
  17.         public ExampleController(DBAccessContext context)  
  18.         {  
  19.             _context = context;  
  20.         }  
  21.   
  22.         // GET: Example  
  23.         public async Task<IActionResult> Index()  
  24.         {  
  25.             return View(await _context.Crud_Data.ToListAsync());  //your logic to get data from database
  26.         }  
  27.   
  28.          
  29.     }  
  30. }  
 Index.cshtml under View/Example folder
  1. @model IEnumerable<TestDemo.Models.CrudModel>  
  2.   
  3. @{  
  4.     ViewData["Title"] = "Index";  
  5.     Layout = "~/Views/Shared/_Layout.cshtml";  
  6. }  
  7.   
  8. <h1>Index</h1>  
  9.   
  10. <p>  
  11.     <a asp-action="Create">Create New</a>  
  12. </p>  
  13.   
  14. <partial name="_ValidationScriptsPartial" />  
  15. <script>  
  16.   
  17.     $(document).ready(function () {  
  18.         $('#table').DataTable({  
  19.             dom: 'Bfrtip',  
  20.             buttons: [  
  21.                 'copy''csv',  'print'  
  22.             ]  
  23.         });  
  24.     });  
  25.   
  26. </script>  
  27. <table class="table table-striped table-bordered" style="width:100%;" id="table">  
  28.     <thead>  
  29.         <tr>  
  30.             <th>  
  31.                 @Html.DisplayNameFor(model => model.Name)  
  32.             </th>  
  33.             <th>  
  34.                 @Html.DisplayNameFor(model => model.City)  
  35.             </th>  
  36.             <th>  
  37.                 @Html.DisplayNameFor(model => model.InsertDate)  
  38.             </th>  
  39.             <th>  
  40.                 @Html.DisplayNameFor(model => model.FatherName)  
  41.             </th>  
  42.   
  43.         </tr>  
  44.     </thead>  
  45.     <tbody>  
  46.         @foreach (var item in Model)  
  47.         {  
  48.             <tr>  
  49.                 <td>  
  50.                     @Html.DisplayFor(modelItem => item.Name)  
  51.                 </td>  
  52.                 <td>  
  53.                     @Html.DisplayFor(modelItem => item.City)  
  54.                 </td>  
  55.                 <td>  
  56.                     @Html.DisplayFor(modelItem => item.InsertDate)  
  57.                 </td>  
  58.                 <td>  
  59.                     @Html.DisplayFor(modelItem => item.FatherName)  
  60.                 </td>  
  61.                 @*<td>  
  62.                         <a asp-action="Edit" asp-route-id="@item.Id">Edit</a> |  
  63.                         <a asp-action="Details" asp-route-id="@item.Id">Details</a> |  
  64.                         <a asp-action="Delete" asp-route-id="@item.Id">Delete</a>  
  65.                     </td>*@  
  66.             </tr>  
  67.         }  
  68.     </tbody>  
  69. </table>  
In _Layout.cshtml, I added a reference for site.min.js and site.min.css which we have registered in bundleconfig.json as an output file. 
  1. <script src="~/js/site.min.js" asp-append-version="true"></script>  
  2.  <link href="~/js/site.min.css" rel="stylesheet" asp-append-version="true" />  
That's it! Let's run our project and see the output.
 
 

Summary

 
Here, we have learned how to show data from the database to our View in .NET Core MVC 3.1.
 
We also used bundling and minification for completing the process. We have shown the Data to the view with the feature of sorting, paging, and exporting.  
 
I hope it helps. Happy Coding