HI WELCOME TO SIRIS

WebGrid with custom paging and sorting in ASP.NET MVC

WebGrid is simply used to display the data with paging and sorting. If you want to control the default behavior of the webgrid, you need do it manually. In this article, I would like to explore how can we do custom paging and sorting. In this article I have used the MVC4 and Entity Framework 5.0 with VS2012. Refer the article Enhancing WebGrid with ajax in MVC4 for more help.

How to do it...

The Model

First of all design the customer model using Entity Framework database first approach as show below;
Now develop the logic for querying the data from customer table and also develop the logic for custom paging and sorting.
  1. public class ModelServices : IDisposable
  2. {
  3. private readonly TestDBEntities entities = new TestDBEntities();
  4. //For Custom Paging
  5. public IEnumerable<Customer> GetCustomerPage(int pageNumber, int pageSize, string sort, bool Dir)
  6. {
  7. if (pageNumber < 1)
  8. pageNumber = 1;
  9. if (sort == "name")
  10. return entities.Customers.OrderByWithDirection(x => x.Name, Dir)
  11. .Skip((pageNumber - 1) * pageSize)
  12. .Take(pageSize)
  13. .ToList();
  14. else if (sort == "address")
  15. return entities.Customers.OrderByWithDirection(x => x.Address, Dir)
  16. .Skip((pageNumber - 1) * pageSize)
  17. .Take(pageSize)
  18. .ToList();
  19. else if (sort == "contactno")
  20. return entities.Customers.OrderByWithDirection(x => x.ContactNo, Dir)
  21. .Skip((pageNumber - 1) * pageSize)
  22. .Take(pageSize)
  23. .ToList();
  24. else
  25. return entities.Customers.OrderByWithDirection(x => x.CustID, Dir)
  26. .Skip((pageNumber - 1) * pageSize)
  27. .Take(pageSize)
  28. .ToList();
  29. }
  30. public int CountCustomer()
  31. {
  32. return entities.Customers.Count();
  33. }
  34. public void Dispose()
  35. {
  36. entities.Dispose();
  37. }
  38. }
  39.  
  40. public class PagedCustomerModel
  41. {
  42. public int TotalRows { get; set; }
  43. public IEnumerable<Customer> Customer { get; set; }
  44. public int PageSize { get; set; }
  45. }

Extension Method for OrderByWithDirection

  1. public static class SortExtension
  2. {
  3. public static IOrderedEnumerable OrderByWithDirection
  4. (this IEnumerable source,Func keySelector,bool descending)
  5. {
  6. return descending ? source.OrderByDescending(keySelector)
  7. : source.OrderBy(keySelector);
  8. }
  9. public static IOrderedQueryable OrderByWithDirection
  10. (this IQueryable source,Expression> keySelector,bool descending)
  11. {
  12. return descending ? source.OrderByDescending(keySelector)
  13. : source.OrderBy(keySelector);
  14. }
  15. }

The View

Now design the view based on the above developed model as show below
  1. @model Mvc4_CustomWebGrid.Models.PagedCustomerModel
  2. @{
  3. ViewBag.Title = "WebGrid with Custom Paging, Sorting";
  4. WebGrid grid = new WebGrid(rowsPerPage: Model.PageSize);
  5. grid.Bind(Model.Customer,
  6. autoSortAndPage: false,
  7. rowCount: Model.TotalRows);
  8. }
  9.  
  10. @grid.GetHtml(
  11. fillEmptyRows: false,
  12. alternatingRowStyle: "alternate-row",
  13. headerStyle: "grid-header",
  14. footerStyle: "grid-footer",
  15. mode: WebGridPagerModes.All,
  16. firstText: "<< First",
  17. previousText: "< Prev",
  18. nextText: "Next >",
  19. lastText: "Last >>",
  20. columns: new[] {
  21. grid.Column("CustID",header: "ID", canSort: false),
  22. grid.Column("Name"),
  23. grid.Column("Address"),
  24. grid.Column("ContactNo",header: "Contact No")
  25. }
  26. )

The Controller

Now, let's see how to write the code for implementing the webgrid functionality using model class and methods.
  1. public class HomeController : Controller
  2. {
  3. ModelServices mobjModel = new ModelServices();
  4. public ActionResult WebGridCustomPaging(int page = 1, string sort = "custid", string sortDir = "ASC")
  5. {
  6. const int pageSize = 5;
  7. var totalRows = mobjModel.CountCustomer();
  8. bool Dir = sortDir.Equals("desc", StringComparison.CurrentCultureIgnoreCase) ? true : false;
  9. var customer = mobjModel.GetCustomerPage(page, pageSize, sort, Dir);
  10. var data = new PagedCustomerModel()
  11. {
  12. TotalRows = totalRows,
  13. PageSize = pageSize,
  14. Customer = customer
  15. };
  16. return View(data);
  17. }
  18. }

How it works..


 
What do you think?
I hope you will enjoy the tricks while programming with MVC Razor. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.