MVC - Searching, Sorting And Paging In Web Grid

Introduction

I was working on a project where I had to implement a Grid with searching, sorting, and paging as per customer requirement.

I used MVC Web Grid for sorting and paging. I implemented searching features using a text box and AJAX post call. I presume that you have basic knowledge of MVC, jQuery and Partial View.

WebGrid: It is GridView. We can show data in tabular format with sorting and paging as inbuilt features.
Searching: Search feature is not available in WebGrid. So, I implemented it using textbox control and jQuery AJAX.

Index.cshtml

Index.cshtml is Main View. It renders PartialView (_GridPartialView.cshtml). Index View has one PartialView and on Search box.

Search action method is getting called using AJAX. I have mention comment for AJAX call for each line.

  1. var searchVal = $(“#txtSearch”).val();   
The above code will get value of search textbox and assign this value to searchString parameter of Search action method.
  1. @model List < MVC.WebGrid.Filter.Pagination.Searching.Models.User > @ {  
  2.     ViewBag.Title = "User";  
  3. } < table > < tr > < td > < input type = "text"  
  4. id = "txtSearch"  
  5. placeholder = "Search..."  
  6. onkeyup = "Search()" / > < /tr> < tr > < td > < div id = "divPartialView" > @Html.Partial("~/Views/Shared/_GridPartialView.cshtml", Model) < /div> < /td> < /tr> < /table> < script type = "text/javascript" > function Search() {  
  7.     var searchVal = $("#txtSearch").val();  
  8.     $.ajax({  
  9.         type: "POST"// POST call  
  10.         url: '/Home/Search'// call search action method of Home controller  
  11.         data: {  
  12.             searchString: searchVal  
  13.         }, // pass the parameter to action method. Here searchString is parameter of Action method from controller.   
  14.         dataType: 'html'// return html to render  
  15.         success: function(data) {  
  16.             $('#divPartialView').html(data); // Render filter result to div where partial view is there. We can render div and partial view should be inside div.  
  17.         }  
  18.     });  
  19. } < /script>  
_GridUserPartialView.cshtml: This is PartialView rendered in index View. PartialView contains WebGrid to display data.

Why PartialView?: It is reusable and can refresh page partially. Only the WebGrid will be refreshed, when you type the search term in textbox.
  1. @model List<MVC.WebGrid.Filter.Pagination.Searching.Models.User>  
  2. <script src="../../Scripts/jquery-1.7.1.min.js" type="text/javascript"></script>  
  3. <style type="text/css">  
  4. .webGrid {  
  5. margin: 4px;  
  6. border-collapse: collapse;  
  7. width: 500px;  
  8. background-color: #FCFCFC;  
  9. }  
  10.   
  11. .header {  
  12. background-color: #C1D4E6;  
  13. font-weight: bold;  
  14. color: #FFF;  
  15. }  
  16.   
  17. .webGrid th, .webGrid td {  
  18. border: 1px solid #C0C0C0;  
  19. padding: 5px;  
  20. }  
  21.   
  22. .alt {  
  23. background-color: #E4E9F5;  
  24. color: #000;  
  25. }  
  26.   
  27. .gridHead a:hover {  
  28. text-decoration: underline;  
  29. }  
  30.   
  31. .description {  
  32. width: auto;  
  33. }  
  34.   
  35. .select {  
  36. background-color: #389DF5;  
  37. }  
  38. </style>  
  39.   
  40. @{  
  41. var grid = new WebGrid(Model, canPage: true, rowsPerPage: 5, selectionFieldName: "selectedRow", ajaxUpdateContainerId: "grid");  
  42. grid.Pager(WebGridPagerModes.NextPrevious);  
  43. }  
  44. <div id="grid">  
  45. @grid.GetHtml(  
  46.   
  47. tableStyle: "webGrid", mode: WebGridPagerModes.All,  
  48.   
  49. firstText: "<< First",  
  50. previousText: "< Prev",  
  51. nextText: "Next >",  
  52. lastText: "Last >>",  
  53. headerStyle: "header",  
  54. alternatingRowStyle: "alt",  
  55. selectedRowStyle: "select",  
  56. columns: grid.Columns(  
  57.   
  58. grid.Column("UserName""User Name", style: "description"),  
  59. grid.Column("FirstName""First Name"),  
  60. grid.Column("LastName""Last Name")  
  61.   
  62. ))  
  63.   </div>  
UserController.cs: This is Search action method inside Usercontroller. It is HTTPPOST call.
  1. [HttpPost]  
  2. public PartialViewResult Search(string searchString)   
  3. {  
  4.     List < User > userListCollection = new List < User > ();  
  5.     userListCollection = GetUSer();  
  6.     if (Request.IsAjaxRequest())   
  7.     {  
  8.         if (!string.IsNullOrEmpty(searchString) && userListCollection != null && userListCollection.Count > 0)   
  9.         {  
  10.             var searchedlist = (from list in userListCollection where list.FirstName.IndexOf(searchString, StringComparison.OrdinalIgnoreCase) >= 0 || list.UserName.IndexOf(searchString, StringComparison.OrdinalIgnoreCase) >= 0 || list.LastName.IndexOf(searchString, StringComparison.OrdinalIgnoreCase) >= 0 select list).ToList();  
  11.             return PartialView("_GridPartialView", searchedlist);  
  12.         } else {  
  13.             return PartialView("_GridPartialView", userListCollection);  
  14.         }  
  15.     } else {  
  16.         return PartialView("_GridPartialView", userListCollection);  
  17.     }  
  18. }  
public PartialViewResult Search(string searchString) : Search is action method and it will get parameter value from AJAX call and searchString parameter is assigned in the AJAX call.

You have to search this string in your collection and return the result to PartialView. You need to pass the filter model to PartialView.

I have completed WebGrid with search, sort, and paging. I have attached source code for your reference.
X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now