Searching And Paging In MVC

Database structure

Create a table in the database with the name Employee.

The following is my table design.



Create MVC Application 

Step 1: Go to File, New, then Project.

Step 2: Go to File, New, Project. Choose "ASP.NET MVC 4 Web Application" from the list, then provide the application name as you wish and set the path in the location input where you want to create the application.

Step 3
: Now choose the Project Template "Basic".

Adding a ADO.Net Entity Data Model

Step 1: Right-click on the project and select "Add new item", then select Data from the templates.

Step 2: Choose "ADO.Net Entity Data Model" from the list and provide a name. Now, after clicking on Add, you can see the .edmx file in the project.

Step 3:


Create Homecontroller

Right click on controller Folder and select template Empty MVC controller.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6. using PagedList;  
  7. using PagedList.Mvc;  
  8.   
  9. namespace searchingdata.Controllers  
  10. {  
  11.     public class HomeController : Controller  
  12.     {  
  13.         Dbcontext db = new Dbcontext();  
  14.         public ActionResult Index(string Searchby, string search)  
  15.         {  
  16.             if (Searchby == "Gender")  
  17.             {  
  18.                 var model = db.Employees.Where(emp => emp.Gender == search || search == null).ToList();  
  19.                 return View(model);  
  20.   
  21.             }  
  22.             else  
  23.             {  
  24.                 var model = db.Employees.Where(emp => emp.Name.StartsWith(search) || search == null).ToList();  
  25.                 return View(model);  
  26.             }  
  27.         }  
  28. }  
  29. }  
Create Index View

Right click on Index method and select Strong -typed view as shown in the following figure.



Index View
  1. @model IEnumerable<searchingdata.Employee>  
  2.   
  3. @{  
  4.     ViewBag.Title = "Index";  
  5. }  
  6. <link href="~/Content/PagedList.css" rel="stylesheet" />  
  7. <div style="font-family:Arial">  
  8.     <h2>Index</h2>  
  9.   
  10.     <p>  
  11.         @Html.ActionLink("Create New", "Create")  
  12.     </p>  
  13.     <p>  
  14.         @using (Html.BeginForm("Index", "Home", FormMethod.Get))  
  15.         {  
  16.             <b> Search by</b> @Html.RadioButton("searchby", "Name", true)<b> Name</b>  
  17.             @Html.RadioButton("searchby", "Gender") <b> Gender</b><br />  
  18.             @Html.TextBox("search") <input type="submit" value=" Search" />  
  19.         }  
  20.     </p>  
  21.   
  22.     <table border="1">  
  23.         <tr>  
  24.             <th>  
  25.                 @Html.DisplayNameFor(model => model.First().Name)  
  26.             </th>  
  27.             <th>  
  28.                 @Html.DisplayNameFor(model => model.First().Gender)  
  29.             </th>  
  30.             <th>  
  31.                 @Html.DisplayNameFor(model => model.First().EmailId)  
  32.             </th>  
  33.             <th>  
  34.                 @Html.DisplayNameFor(model => model.First().City)  
  35.             </th>  
  36.             <th> Action</th>  
  37.         </tr>  
  38.   
  39.         @if (Model.Count() == 0)  
  40.         {  
  41.   
  42.             <tr>  
  43.                 <td colspan="5">  
  44.                     No record found  
  45.                 </td>  
  46.             </tr>  
  47.   
  48.         }  
  49.         else  
  50.         {  
  51.             foreach (var item in Model)  
  52.             {  
  53.                 <tr>  
  54.                     <td>  
  55.                         @Html.DisplayFor(modelItem => item.Name)  
  56.                     </td>  
  57.                     <td>  
  58.                         @Html.DisplayFor(modelItem => item.Gender)  
  59.                     </td>  
  60.                     <td>  
  61.                         @Html.DisplayFor(modelItem => item.EmailId)  
  62.                     </td>  
  63.                     <td>  
  64.                         @Html.DisplayFor(modelItem => item.City)  
  65.                     </td>  
  66.                     <td>  
  67.                         @Html.ActionLink("Edit", "Edit", new { id = item.ID }) |  
  68.                         @Html.ActionLink("Details", "Details", new { id = item.ID }) |  
  69.                         @Html.ActionLink("Delete", "Delete", new { id = item.ID })  
  70.                     </td>  
  71.                 </tr>  
  72.             }  
  73.         }  
  74.   
  75.     </table>  
  76. </div>  
In above code I have two html radiobutton and one html text box and simple submit Button.

Press F5 and run you application.

Here I have checked the name Radiobutton and searched name starting from S alphabet. The output is shown in the following figure.


Here I have checked Gender RadioButton and searched gender male. The output is shown in the following figure.


Now I am adding paging concept in this project. First we need to add PagedList.mvc package in our project. The process is shown in the following figure.

 

Now see your project we have library in our project PagedList and PagedList.mvc and we have also got one Css file inside content folder with the name PagedList.css.

Add new parameter in Index Method: inside Home controller, add int? page is a nullable parameter. Let's see the updated Index.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6. using PagedList;  
  7. using PagedList.Mvc;  
  8.   
  9. namespace searchingdata.Controllers  
  10. {  
  11.     public class HomeController : Controller  
  12.     {  
  13.         Dbcontext db = new Dbcontext();  
  14.         
  15.   
  16.         public ActionResult Index(string Searchby, string search, int? page)  
  17.         {  
  18.             if (Searchby == "Gender")  
  19.             {  
  20.                 var model = db.Employees.Where(emp => emp.Gender == search || search == null).ToList().ToPagedList(page??1 ,3);  
  21.                 return View(model);  
  22.   
  23.             }  
  24.             else  
  25.             {  
  26.                 var model = db.Employees.Where(emp => emp.Name.StartsWith(search) || search == null).ToList().ToPagedList(page ?? 1, 3);  
  27.                 return View(model);  
  28.             }  
  29.         }  
  30.             
  31.   
  32.     }  
  33. }  
In the above code to return PagedList(), and page list contains two parameters int pagenumber and int pagesize.

If you want to show the number of rows displayed of the total number of rows available,

 

  1. @Html.PagedListPager(Model, page => Url.Action("Index", new { page, searchBy = Request.QueryString["searchBy"], search = Request.QueryString["search"] }), new PagedListRenderOptions() { Display = PagedListDisplayMode.IfNeeded, DisplayItemSliceAndTotal = true })  

 

  1. @using PagedList;  
  2. @using PagedList.Mvc;  
  3.   
  4. @model IPagedList<searchingdata.Employee>  
  5.   
  6. @{  
  7.     ViewBag.Title = "Index";  
  8. }  
  9. <link href="~/Content/PagedList.css" rel="stylesheet" />  
  10. <div style="font-family:Arial">  
  11.     <h2>Index</h2>  
  12.   
  13.     <p>  
  14.         @Html.ActionLink("Create New", "Create")  
  15.     </p>  
  16.     <p>  
  17.         @using (Html.BeginForm("Index", "Home", FormMethod.Get))  
  18.         {  
  19.             <b> Search by</b> @Html.RadioButton("searchby", "Name", true)<b> Name</b>  
  20.             @Html.RadioButton("searchby", "Gender") <b> Gender</b><br />  
  21.             @Html.TextBox("search") <input type="submit" value=" Search" />  
  22.         }  
  23.     </p>  
  24.   
  25.     <table border="1">  
  26.         <tr>  
  27.             <th>  
  28.                 @Html.DisplayNameFor(model => model.First().Name)  
  29.             </th>  
  30.             <th>  
  31.                 @Html.DisplayNameFor(model => model.First().Gender)  
  32.             </th>  
  33.             <th>  
  34.                 @Html.DisplayNameFor(model => model.First().EmailId)  
  35.             </th>  
  36.             <th>  
  37.                 @Html.DisplayNameFor(model => model.First().City)  
  38.             </th>  
  39.             <th> Action</th>  
  40.         </tr>  
  41.   
  42.         @if (Model.Count() == 0)  
  43.         {  
  44.   
  45.             <tr>  
  46.                 <td colspan="5">  
  47.                     No record found  
  48.                 </td>  
  49.             </tr>  
  50.   
  51.         }  
  52.         else  
  53.         {  
  54.             foreach (var item in Model)  
  55.             {  
  56.                 <tr>  
  57.                     <td>  
  58.                         @Html.DisplayFor(modelItem => item.Name)  
  59.                     </td>  
  60.                     <td>  
  61.                         @Html.DisplayFor(modelItem => item.Gender)  
  62.                     </td>  
  63.                     <td>  
  64.                         @Html.DisplayFor(modelItem => item.EmailId)  
  65.                     </td>  
  66.                     <td>  
  67.                         @Html.DisplayFor(modelItem => item.City)  
  68.                     </td>  
  69.                     <td>  
  70.                         @Html.ActionLink("Edit", "Edit", new { id = item.ID }) |  
  71.                         @Html.ActionLink("Details", "Details", new { id = item.ID }) |  
  72.                         @Html.ActionLink("Delete", "Delete", new { id = item.ID })  
  73.                     </td>  
  74.                 </tr>  
  75.             }  
  76.         }  
  77.   
  78.     </table>  
  79. </div>  
  80. <div id='Paging'>  
  81.   
  82.     @Html.PagedListPager(Model, page => Url.Action( "Index", new { page, searchby = Request.QueryString["searchby"], search = Request.QueryString["search"] }), new PagedListRenderOptions() { Display = PagedListDisplayMode.IfNeeded, DisplayPageCountAndCurrentLocation = trueDisplayItemSliceAndTotal = true })  
  83. </div>  

Run your application - Press F5.

Summary

In this article we learned about searching and paging in MVC using Entity Framework. 

Up Next
    Ebook Download
    View all
    Learn
    View all