Saturday, August 10, 2019

How to implement Custom Paging in webgrid in MVC4 application

Here We will see how to implement Custom Paging in webgrid in MVC4 application. It is a very essential approach to use paging technique in applications where lot of data to be loaded from database.

Steps :

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 show data in webgrid.

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

  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using System.Web.Mvc;
  4. namespace MVCWebgridPaging.Controllers
  5. {
  6. public class WebgridController : Controller
  7. {
  8. //
  9. // GET: /Webgrid/
  10.  
  11. public ActionResult CustomPaging(int page = 1)
  12. {
  13. int pageSize = 5;
  14. int totalPage = 0;
  15. int totalRecord = 0;
  16. List<CustomerInfo> allCustomer = new List<CustomerInfo>();
  17. using (MyDatabaseEntities dc =new MyDatabaseEntities())
  18. {
  19. // here MyDatabaseEntities is our DbContext //
  20. totalRecord = dc.CustomerInfoes.Count();
  21. totalPage = (totalRecord / pageSize) + ((totalRecord % pageSize) > 0 ? 1 : 0);
  22. allCustomer = dc.CustomerInfoes.OrderBy(a => a.CustomerID).Skip(((page - 1) * pageSize)).Take(pageSize).ToList();
  23. }
  24.  
  25. ViewBag.TotalRows = totalRecord;
  26. ViewBag.PageSize = pageSize;
  27.  
  28. return View(allCustomer);
  29. }
  30. }
  31. }
  32.  

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) > Check "Create a strong-typed view" > Select your model class > Add.
[N:B:Please Rebuild solution before add view.] 
Complete View 
  1. @model List<MVCWebgridPaging.CustomerInfo>
  2.  
  3. @{
  4. ViewBag.Title = "Custom Paging";
  5. // Here for used webgrid we have to add Helper dll as we used Basic MVC templete its by default not exist
  6. var grid = new WebGrid(rowsPerPage: ViewBag.PageSize, canPage: true);
  7. grid.Bind(source: Model, rowCount: ViewBag.TotalRows, autoSortAndPage: false);
  8. }
  9.  
  10. @* Here I will add some style for grid *@
  11. <style type="text/css">
  12. table.gridtable {
  13. font-family: verdana,arial,sans-serif;
  14. font-size:11px;
  15. color:#333333;
  16. border-width: 1px;
  17. border-color: #666666;
  18. border-collapse: collapse;
  19. }
  20. table.gridtable th {
  21. border-width: 1px;
  22. padding: 8px;
  23. border-style: solid;
  24. border-color: #666666;
  25. background-color: #dedede;
  26. }
  27. table.gridtable td {
  28. border-width: 1px;
  29. padding: 8px;
  30. border-style: solid;
  31. border-color: #666666;
  32. background-color: #ffffff;
  33. }
  34. </style>
  35.  
  36. <h2>Custom Paging</h2>
  37.  
  38. <div>
  39. @grid.GetHtml(tableStyle:"gridtable",
  40. columns: grid.Columns(
  41. grid.Column("CustomerID","Customer ID"),
  42. grid.Column("CustomerName","Customer Name"),
  43. grid.Column("Address","Address"),
  44. grid.Column("City","City"),
  45. grid.Column("PostalCode","Postal Code")
  46. )
  47. )
  48. </div>
Step-8: 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 { ...