Skip to main content

Implement paging in Asp.net MVC using PagedList library

Introduction:


 In this article, I will explain How to implement paging using PagedList library in Asp.net MVC.

Step 1: Open visual studio à Go to File menu àselect New à Project like as shown below


Step 2: Now from web templates select Asp.Net Web Application (.NET Framework) and give name (PagingInMVC) to the project and click OK button like as shown below.

Step 3: Once we click OK button new template will open in that select MVC from templates like as shown below

Step 4:Our project structure will be like as shown below


step 5: Delete the HomeController already present in the project because we will create it from scratch as shown below

Step 6: Delete the Home folder inside Views folder as shown below

Step 7 : Click on Models -> Add ->New Item as shown in below

Step 8: Select Data -> ADO.NET Entity Data Model and give name PagingModal and click add as shown below


Step 9: Select EF Designer from Database and click next as shown below

Step 10 : Enter server name (local) or your live server name and select database name and test the connection and click Ok as shown below

Step 11 : Click  Next as shown below

Step 12 : Select Next  as shown below

Step 13 : Tick check box like as shown below and click finish

Step 14 : Our models folder looks like as shown below

Step 15 :
The DummyDBEntities class is derived from DbContext class and contains DbSet<TEntity> properties of tbl_Employee type. It also overrides OnConfiguring and OnModelCreating methods. We must create an instance of DummyDBEntities to connect to the database and save or   to retrieve tbl_Employee data. 

Step 16 Our model class looks like as shown below.In database first approach this code is automatically generated by visual studio from database table.

Step 17 : Connection string is auto generated in database first approach which is shown below 

Step 18 : Right click on controller folder ->Add ->Controller as shown below

Step 19 : Dialogue box will open once you click on controller and select MVC 5 controller - Empty and click Add as shown below 

Step 20 : Rename as HomeController and click Add button.Don't remove controller after Home because it is convention of MVC and MVC works on convention.

Step 21 : Now it is time to install PagedList library through Nuget package manager and for this right click on Project or Solution -> Manage Nuget Packages as shown below

Step 22 : Type PagedList in search bar and install PagedList.MVC and PagedList automatically install as shown below

Step 23 : Replace your HomeController code with following code 
 using System;  
 using System.Collections.Generic;  
 using System.Linq;  
 using System.Web;  
 using System.Web.Mvc;  
 using PagedList;  
 namespace PagingInMVC.Controllers  
 {  
   public class HomeController : Controller  
   {  
     DummyDBEntities dummyDBEntities = new DummyDBEntities();  
     // GET: Home  
     public ActionResult Index(int ? page,int pageSize=2)  
     {  
       int pageNumber = page ?? 1;  
       var onePageOfEmployees = dummyDBEntities.tbl_Employee.ToList().ToPagedList(pageNumber, pageSize);  
       if(Request.IsAjaxRequest())  
       {  
         return PartialView("_EmployeeGrid", onePageOfEmployees);  
       }  
       return View(onePageOfEmployees);  
     }  
   }
 }  
Step 24 : Rebuild the project as shown below


Step 25 : Right click inside Index action and dialogue box will open and choose Add View.


Step 26 :Select List from template,model class,data context class and layout page and click on Add as shown below

Step 27 : Again go to Nuget package Manager and install 
Microsoft.jQuery.Unobtrusive.Ajax as shown below


Step 28 : Go to Views->Home->Index.cshtml and replace this code 
@using PagedList
@using PagedList.Mvc
@model IPagedList<PagingInMVC.Models.tbl_Employee>

@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<div id="employee-grid">
    @{ 
        Html.RenderPartial("_EmployeeGrid", Model);
    };
</div>
@section scripts{
   
<script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>
    }
Step 29: Views -> Home->Right click on Home->Add->Add View->select create as partial view check box and add partial view with empty template and paste the following code 
@using PagedList
@using PagedList.Mvc
@model IPagedList<PagingInMVC.Models.tbl_Employee>
<table class="table">
    <tr>
        <th>
            Employee Name
        </th>
        <th>
            Salary
        </th>
        <th></th>
    </tr>

    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.EmployeeName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Salary)
            </td>
            <td>
                @Html.ActionLink("Edit", "Edit", new { id = item.EmpID }) |
                @Html.ActionLink("Details", "Details", new { id = item.EmpID }) |
                @Html.ActionLink("Delete", "Delete", new { id = item.EmpID })
            </td>
        </tr>
    }

</table>
@Html.PagedListPager(Model, page => Url.Action("Index", new { page=page}),PagedListRenderOptions.EnableUnobtrusiveAjaxReplacing(new AjaxOptions() {HttpMethod="GET",UpdateTargetId= "employee-grid" }))
Step 30 : Run your project and output is shown below 



Happy Coding !!!

Comments

Popular posts from this blog

Daphne array

Question : A  Daphne array  is an array that contains either all odd numbers or all even numbers. For example, {2, 4, 2} (only even numbers) and {1, 3, 17, -5} (only odd numbers) are Daphne arrays but {3, 2, 5} is not because it contains both odd and even numbers. Write a function named  isDaphne  that returns 1 if its array argument is a Daphne array. Otherwise it returns 0. If you are programming in Java or C#, the function prototype is    int isDaphne (int[ ] a); If you are programming in C or C++, the function prototype is    int isDaphne (int a[ ], int len) where len is the number of elements in the array. Solution : public int isDaphne ( int [] a) { bool isEven = false , isOdd = false ; for ( int i= 0 ;i<a.Length;i++) { if (a[i]% 2 == 0 ) { isEven = true ; } else { ...

wave array

A   wave   array   is defined to an array which does   not   contain   two even numbers   or two odd numbers in adjacent locations. So {7, 2, 9, 10, 5}, {4, 11, 12, 1, 6},   {1, 0, 5} and {2} are all   wave arrays . But { 2, 6 , 3, 4} is not a   wave   array because the even numbers 2 and 6 are adjacent to each other.   Also   {3, 4, 9, 11, 8}   is not a wave array because the   odd   numbers   9   and   11   are adjacent to each other.   You can assume array has at least one element.            Write a function   named   isWave   that returns 1 if its array argument is a   Wave   array, otherwise it returns 0. If you are programming in Java or C#, the function signature is    int isWave   (int [ ] a) If you are programming in C or C++, the function signature is    int is...

minDistance

Write a function named   minDistance   that returns the smallest distance between two   non-trivial   factors of   a number. For example, consider   63. Its   non-trivialfactors are 3,   7, 9   and 21. Thus   minDistance (63) would return 2 because the   smallest distance between any two   non-trivial factors is 2 (9 - 7   = 2).   As another example,   minDistance   (25) would return 0   because   25 has only   one   non-trivial   factor:   5. Thus   the smallest distance   between any two   non-trivial   factors is 0   (5   -   5 = 0).   Note that   minDistance (11)   would return -1 since 11 has no non-trivial factors. The function signature is    int   minDistance (int n)  Solution :        public static int minDistance ( int n) { int minDist = n, co...