Saturday, August 10, 2019

How to implement Custom Paging and sorting in webgrid using jquery

Step - 1 : Create New Project.

Go to File > New > Project > Select asp.net MVC4 web application > Entry Application Name > Click OK > Select Internet Application > Select view engine Razor > OK

Step-2: Add a Database.

Go to Solution Explorer > Right Click on App_Data folder > Add > New item > Select SQL Server Database Under Data > Enter Database name > Add.

Step-3: Create table for fetch data.

Open Database > Right Click on Table > Add New Table > Add Columns > Save > Enter table name > Ok.
In this example, I have used one tables as below

Step-4: Add Entity Data Model.

Go to Solution Explorer > Right Click on Project name form Solution Explorer > Add > New item > Select ADO.net Entity Data Model under data > Enter model name > Add.
A popup window will come (Entity Data Model Wizard) > Select Generate from database > Next >


Chose your data connection > select your database > next > Select tables > enter Model Namespace > Finish.

Step-5: Add a new Controller.

Go to Solution Explorer > Right Click on Controllers folder form Solution Explorer > Add > Controller > Enter Controller name > Select Templete "empty MVC Controller"> Add.

Step-6: Add new action into your controller for get the view for display data.

Here I have added "Index" Action into "Webgrid" Controller. Please write this following code

  1. public ActionResult List()
  2. {
  3. return View();
  4. }

Step-7: Add view for the Action & design.

Right Click on Action Method (here right click on form action) > Add View... > Enter View Name > Select View Engine (Razor) > Add.
Complete View 
  1. @{
  2. ViewBag.Title = "List";
  3. }
  4.  
  5. <h2>Customer List</h2>
  6. <div id="demoArea">
  7. @{Html.RenderAction("GetCustomerData", "Webgrid");}
  8. </div>
  9.  
  10. @* Here I will write jquery code for webgrid paging and sorting *@
  11.  
  12. @section Scripts{
  13. <script>
  14. $(document).ready(function () {
  15.  
  16. //thead th a ---> webgrid header link (sorting) , tfoot a --> webgrid footer link (paging)
  17.  
  18. $("#demoArea").on("click", "thead th a, tfoot a", function (e) {
  19. e.preventDefault();
  20. var param = $(this).attr('href').split('?')[1];
  21. var url = '@Url.Action("GetCustomerData","Webgrid")' + '?' + param;
  22. $.ajax({
  23. url: url,
  24. type:'GET',
  25. data: '',
  26. dataType: 'html',
  27. success: function (data) {
  28. $('#demoArea').html(data);
  29. },
  30. error: function () {
  31. alert('Error!');
  32. }
  33. });
  34. });
  35. });
  36. </script>
  37. }

Step-8: Add a new Class (Model).

Right Click on Models Folder > Add > New > Class > Enter Class Name > Add.
Write following code...

  1. using System.Collections.Generic;
  2. namespace MVCSortingPagingJquery.Models
  3. {
  4. public class CustomerDataModel
  5. {
  6. public List<CustomerInfo> Customer { get; set; }
  7. public int PageSize { get; set; }
  8. public int TotalRecord { get; set; }
  9. public int NoOfPages { get; set; }
  10. }
  11. }

Step-9: Add Reference of System.Linq.Dynamic from a new Class (Model).

Right Click on References > Manage NuGet Packages > Seach online with "System.Linq.Dynamic" keyword > Select & install System.Linq.Dynamic

Step-10: Add another action for get partial view with filtered Data.

Write following code...

  1. public ActionResult GetCustomerData(int page = 1, string sort = "CustomerName", string sortdir = "ASC")
  2. {
  3. CustomerDataModel cdm = new CustomerDataModel();
  4. cdm.PageSize = 4;
  5. using (MyDatabaseEntities dc = new MyDatabaseEntities())
  6. {
  7. cdm.TotalRecord = dc.CustomerInfoes.Count();
  8. cdm.NoOfPages = (cdm.TotalRecord / cdm.PageSize) + ((cdm.TotalRecord % cdm.PageSize) > 0 ? 1 : 0);
  9. cdm.Customer = dc.CustomerInfoes.OrderBy(sort + " " + sortdir).Skip((page - 1) * cdm.PageSize).Take(cdm.PageSize).ToList();
  10. }
  11. return PartialView("_dataList", cdm);
  12. }

Step-11: Add Partial View & design.

Right Click on Shared folder > Add > View > Enter View Name > Checked "Create a Strongly-Typed view" > Select Model Class > Checked "Create as a partial view" > Add.
[N:B:Please Rebuild solution before add view.] 
Complete View 
  1. @model MVCSortingPagingJquery.Models.CustomerDataModel
  2. @{
  3. var grid = new WebGrid(rowsPerPage: Model.PageSize);
  4. grid.Bind(Model.Customer, autoSortAndPage: false, rowCount: Model.TotalRecord);
  5. }
  6.  
  7. @grid.GetHtml(
  8. tableStyle:"gridtable",
  9. columns: grid.Columns(
  10. grid.Column("CustomerID","Customer ID"),
  11. grid.Column("CustomerName", "Customer Name"),
  12. grid.Column("Address", "Address"),
  13. grid.Column("City", "City"),
  14. grid.Column("PostalCode", "Postal Code")
  15. )
  16. )
  17.  
  18. @* Css for good looks of webgrid *@
  19. <style type="text/css">
  20. table.gridtable {
  21. font-family: verdana,arial,sans-serif;
  22. font-size:11px;
  23. color:#333333;
  24. border-width: 1px;
  25. border-color: #666666;
  26. border-collapse: collapse;
  27. }
  28. table.gridtable th {
  29. border-width: 1px;
  30. padding: 8px;
  31. border-style: solid;
  32. border-color: #666666;
  33. background-color: #dedede;
  34. }
  35. table.gridtable td {
  36. border-width: 1px;
  37. padding: 8px;
  38. border-style: solid;
  39. border-color: #666666;
  40. background-color: #ffffff;
  41. }
  42. </style>
Step-12: Run Application.

No comments:

Post a Comment

How to register multiple implementations of the same interface in Asp.Net Core?

 Problem: I have services that are derived from the same interface. public interface IService { } public class ServiceA : IService { ...