---
title: "How to Paging with the entity framework is an ASP.NET MVC Application"  
description: "How to Paging with the entity framework is an ASP.NET MVC Application"  
author: "Anupam Mishra"  
published: 2016-02-05  
updated: 2016-02-05  
canonical: https://www.mindstick.com/forum/33959/how-to-paging-with-the-entity-framework-is-an-asp-dot-net-mvc-application  
category: "asp.net"  
tags: ["c#", "asp.net", "asp.net mvc"]  
reading_time: 4 minutes  

---

# How to Paging with the entity framework is an ASP.NET MVC Application

Hi Everyone,\
I want to know how to [Paging](https://www.mindstick.com/articles/335974/retrieve-data-from-restful-api-and-add-paging-in-knockout-js) with the using of [entity](https://www.mindstick.com/forum/160562/database-connectivity-using-entity-framework-database-first-approach) [framework in ASP.NET](https://www.mindstick.com/forum/158854/what-is-the-purpose-of-the-razor-pages-framework-in-asp-dot-net-core) MVC. I want to display a record in [index](https://www.mindstick.com/blog/198/index-in-sql-server) view with page number .Can [anyone give me](https://answers.mindstick.com/qa/41194/can-anyone-give-me-some-high-and-low-points-of-each-of-the-four-first-presidential-administrations) a solution.\
Thank you.\

## Replies

### Reply by Anupam Mishra

Finally, I have resolve this problem.For Solution of above problem, we are creating one student controller and its index view. We want to display a paging number of records(i.e. First Name,Last Name or Enrollment date) in index view. We are also say, it is complete code of searching, sorting ,filtering and paging example in an [asp.net mvc](https://www.mindstick.com/forum/155798/what-is-caching-in-asp-dot-net-mvc) 4. For paging we want to download one references i.e. PagedList.\
The NuGet PagedList.Mvc package automatically installs the PagedList package as a dependency. The PagedList package installs a PagedList collection type and extension methods for IQueryable and IEnumerable collections. From the Tools menu, select Library Package Manager and then Manage NuGet Packages for Solution.this is a code of Student Controller Index Action:

```
 public ViewResult Index(string sortOrder, string currentFilter, string searchString, int? page)        {            ViewBag.CurrentSort = sortOrder;            ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "";            ViewBag.DateSortParm = sortOrder == "Date" ? "date_desc" : "Date";            if (searchString != null)            {                page = 1;            }            else            {                searchString = currentFilter;            }            ViewBag.CurrentFilter = searchString;            var students = from s in db.Student                           select s;            if (!String.IsNullOrEmpty(searchString))            {                students = students.Where(s => s.FirstName.ToUpper().Contains (searchString.ToUpper())                                       ||s.LastName.ToUpper().Contains(searchString.ToUpper()));            }            switch (sortOrder)            {                case "name_desc":                    students = students.OrderByDescending(s => s.LastName);                    break;                case "Date":                    students = students.OrderBy(s => s.EnrollmentDate);                    break;                case "date_desc":                    students = students.OrderByDescending(s => s.EnrollmentDate);                    break;                default:  // Name ascending                     students = students.OrderBy(s => s.LastName);                    break;            }
```

Now we will change in a student index view. code is as follows:\

```
@model PagedList.IPagedList<StudentUniversity.Models.Student>@using PagedList.Mvc;<link href="~/Content/PagedList.css" rel="stylesheet" />@{    ViewBag.Title = "Index";}    <h2>Index</h2><p>    @Html.ActionLink("Create New",  "Create")    @using (Html.BeginForm("Index", "Student", FormMethod.Get)){    <p>        Find by name: @Html.TextBox("SearchString", ViewBag.CurrentFilter as string)          <input type="submit" value="Search" />    </p>}<table>   <tr>        <th>            @Html.ActionLink("First Name", "Index", new { sortOrder = ViewBag.NameSortParm })        </th>        <th>Last Name        </th>        <th>            @Html.ActionLink("Enrollment Date", "Index", new { sortOrder = ViewBag.DateSortParm })        </th>       <th>Full Name        </th>        <th></th>       </tr>@foreach (var item in Model) {    <tr>        <td>            @Html.DisplayFor(modelItem => item.FirstName)        </td>        <td>            @Html.DisplayFor(modelItem => item.LastName)        </td>        <td>            @Html.DisplayFor(modelItem => item.EnrollmentDate)        </td>        <td>            @Html.DisplayFor(modelItem => item.FullName)        </td>        <td>            @Html.ActionLink("Edit", "Edit", new { id=item.StudentID }) |            @Html.ActionLink("Details", "Details", new { id=item.StudentID }) |            @Html.ActionLink("Delete", "Delete", new { id=item.StudentID })        </td>    </tr>}</table><br/>Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount@Html.PagedListPager( Model, page => Url.Action("Index", new { page, sortOrder = ViewBag.CurrentSort, currentFilter=ViewBag.CurrentFilter }) )
```


---

Original Source: https://www.mindstick.com/forum/33959/how-to-paging-with-the-entity-framework-is-an-asp-dot-net-mvc-application

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
