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 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
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. @if(@Model!=null)
  10. {
  11. <br />
  12. <br />
  13. <table id="tblGrid">
  14. <thead>
  15. <tr>
  16. <th>No</th>
  17. <th>Name</th>
  18. <th>Description</th>
  19. <th>Date</th>
  20. </tr>
  21. </thead>
  22. <tbody>
  23. @foreach (var data in @Model.GridData)
  24. {
  25. <tr>
  26. <td>@data.Id </td>
  27. <td>@data.Name </td>
  28. <td>@data.Desc </td>
  29. <td>@data.Date </td>
  30. </tr>
  31. }
  32. </tbody>
  33. </table>
  34. }
  35. <script>
  36. $(document).ready(function () {
  37. gridDataTableView("Grid Example.xls");
  38. });
  39. gridDataTableView = function (fname) {
  40. $('#tblGrid').dataTable({
  41. "bFilter": true,
  42. "bLengthChange": false, "bPaginate": true, "bInfo": true,
  43. dom: 'Bfrtip',
  44. "order": [[0, 'desc'], [1, 'desc']],
  45. buttons: [{ extend: 'excelHtml5', text: 'Export to Excel', className: 'link_button', title: fname }]
  46. });
  47. }
  48. </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.