Creating Grid View In ASP.NET MVC With Search, Pagination And Export To Excel

In this example, create an ASP.Net MVC project by selecting File -> New -> Project from Visual Studio. Now you can see the below window popped up, from that select “Web” template from the left panel and from the right panel select “ASP.Net Web Application (.Net Framework)” then give the name for the project, select the path, and click “OK” to continue.
 
Creating Grid View In ASP.NET MVC With Search, Pagination And Export To Excel 

After that you can see one more window, from that select “Empty” Template and select MVC as Core references as shown below,

Creating Grid View In ASP.NET MVC With Search, Pagination And Export To Excel 

Now you can see the solution structure as shown below,

Creating Grid View In ASP.NET MVC With Search, Pagination And Export To Excel 
  • Now let’s begin coding. First create model by right clicking on Models folder and select Add -> Class and give some name to that. For my example, I have created 2 classes as shown below:
    1. public class GridModel  
    2. {  
    3.     public int Id { getset; }  
    4.     public string Name { getset; }  
    5.   
    6.     public string Desc { getset; }  
    7.     public string Date { getset; }  
    8. }  
    9. public class GridModelList  
    10. {  
    11.     public List<GridModel> GridData { getset; }  
    12. }  
  • Next Create a controller to use this model and pass to the view, to create a controller right click on the Controllers folder and select Add -> Controller, after that you can see a window shown below is popped up, from that select the first one (MVC 5 Controller- Empty) , then click on Add.

    Creating Grid View In ASP.NET MVC With Search, Pagination And Export To Excel

Now you can see a small pop up to give a name as shown below, provide name and click Add to continue.

Creating Grid View In ASP.NET MVC With Search, Pagination And Export To Excel
  • Inside controller write an action method to fill the “GridModelList”, for this I am using the default-created method “Index” as shown below.
    1. public ActionResult Index()  
    2.   {  
    3.       GridModelList glm = new GridModelList();  
    4.       List<GridModel> gl = new List<GridModel>();  
    5.       GridModel gm = new GridModel();  
    6.       gm.Id = 1;  
    7.       gm.Name = "Test1";  
    8.       gm.Desc = "Test 1 Description";  
    9.       gm.Date = "29-03-2019";  
    10.       gl.Add(gm);  
    11.   
    12.       GridModel gm1 = new GridModel();  
    13.       gm1.Id = 2;  
    14.       gm1.Name = "Test2";  
    15.       gm1.Desc = "Test 2 Description";  
    16.       gm1.Date = "30-03-2019";  
    17.       gl.Add(gm1);  
    18.   
    19.       GridModel gm2 = new GridModel();  
    20.       gm2.Id = 3;  
    21.       gm2.Name = "Test3";  
    22.       gm2.Desc = "Test 3 Description";  
    23.       gm2.Date = "01-04-2019";  
    24.       gl.Add(gm2);  
    25.   
    26.       glm.GridData = gl;  
    27.       
    28.       return View();  
    29.   }  
  • Next we need to create the View and call that View by passing this model (ex: GridModelList) from the Controller. To create view select “Views” folder or inside “Views” folder you may see the folder name as your controller name (as per my example “Grid” ) is created, Right click on that and select Add -> View then you can see a window has opened. Fill the window by giving name, selecting Empty Template, and selecting Model as show below.

    Creating Grid View In ASP.NET MVC With Search, Pagination And Export To Excel
  • After adding View, design the view using model as shown below,
    1. @model MVCGridExample.Models.GridModelList  
    2.    
    3. @if(@Model!=null)  
    4. {  
    5.     <br />  
    6.     <br />  
    7.    
    8.     <table id="tblGrid">  
    9.         <thead>  
    10.             <tr>  
    11.                 <th>No</th>  
    12.                 <th>Name</th>  
    13.                 <th>Description</th>  
    14.                 <th>Date</th>   
    15.             </tr>  
    16.         </thead>  
    17.         <tbody>  
    18.              
    19.                 @foreach (var data in @Model.GridData)  
    20.                 {  
    21.                     <tr>  
    22.                         <td>@data.Id </td>  
    23.                         <td>@data.Name </td>  
    24.                         <td>@data.Desc </td>  
    25.                         <td>@data.Date </td>  
    26.                     </tr>  
    27.                 }  
    28.              
    29.         </tbody>  
    30.     </table>  
  • After view is ready, go to controller and call the view with model as shown below,
    1. return View("~/Views/Grid/GridView.cshtml",glm);  
  • Now run and check. The design will look as shown below,

    Creating Grid View In ASP.NET MVC With Search, Pagination And Export To Excel
     As you can see, the above design does not look good, and there is no option to search or export the data. To make this table design a grid view and give options to search and export data we can use Table plugin for JQuery from here.
After downloading files, add .js files into scripts -> Grid folder and add “jquery.dataTables.min.css” file into Content folder as shown below,
 
Creating Grid View In ASP.NET MVC With Search, Pagination And Export To Excel

Next add these script references into view and call datatable function inside script by adding export button.

The full code is as  follows,

  1. @model MVCGridExample.Models.GridModelList  
  2. <script src="~/scripts/jquery-1.12.4.min.js"></script>  
  3. <script src="~/scripts/Grid/jquery.dataTables.min.js"></script>  
  4. <script src="~/scripts/Grid/jszip.min.js"></script>  
  5. <script src="~/scripts/Grid/dataTables.buttons.min.js"></script>  
  6. <script src="~/scripts/Grid/buttons.html5.min.js"></script>  
  7. <script src="~/scripts/Grid/jquery-ui.min.js"></script>  
  8. <link href="~/Content/jquery.dataTables.min.css" rel="stylesheet" />  
  9.    
  10. @if(@Model!=null)  
  11. {  
  12.     <br />  
  13.     <br />  
  14.    
  15.     <table id="tblGrid">  
  16.         <thead>  
  17.             <tr>  
  18.                 <th>No</th>  
  19.                 <th>Name</th>  
  20.                 <th>Description</th>  
  21.                 <th>Date</th>   
  22.             </tr>  
  23.         </thead>  
  24.         <tbody>  
  25.              
  26.                 @foreach (var data in @Model.GridData)  
  27.                 {  
  28.                     <tr>  
  29.                         <td>@data.Id </td>  
  30.                         <td>@data.Name </td>  
  31.                         <td>@data.Desc </td>  
  32.                         <td>@data.Date </td>  
  33.                     </tr>  
  34.                 }  
  35.              
  36.         </tbody>  
  37.     </table>  
  38. }   
  39. <script>     
  40.      
  41.     $(document).ready(function () {        
  42.        gridDataTableView("Grid Example.xls");        
  43.     });  
  44.      
  45.     gridDataTableView = function (fname) {  
  46.         $('#tblGrid').dataTable({  
  47.             "bFilter"true,  
  48.             "bLengthChange"false"bPaginate"true"bInfo"true,  
  49.             dom: 'Bfrtip',              
  50.             "order": [[0, 'desc'], [1, 'desc']],  
  51.             buttons: [{ extend: 'excelHtml5', text: 'Export to Excel', className: 'link_button', title: fname }]  
  52.         });  
  53.     }  
  54. </script>  
 Here above in datatable for the JQuery function:
  • "bfilter" is used to give filter option for grid view, if you do not want filter option just make it false.
  • "bPaginate" is used to make pagination.
  • "bInfo" is used to show how many records are displaying out of total record in grid view, example here "Showing 1 to 3 of 3 entries".
  • "order" is used to order the data based on column ascending or descending, here we're ordering first and second column in descending order
The output will look as follows:
 
Creating Grid View In ASP.NET MVC With Search, Pagination And Export To Excel
  • Using Export to Excel you can export grid data into excel file·
  •  Using search box you can search data.
  •  When you click on headings you can sort by that header data.
This is just an example; there is  even more you can do with this datatable JQuery.
For more examples click here.


Similar Articles