Hi, again this is Part 2 from Paging in ASP.NET MVC.

In Part 1, I showed you how to add all data required.

Now let's try to finish what we start together.

Now will try to add new Controller and Call it StudentController.

The most important one is, what I highlight it (Yellow).

First one is declared our Interface,

  private readonly IStudent repositoryStudent;

building contractor.

 public StudentController(IStudent repoStudent)
            repositoryStudent = repoStudent;


This one our main job :)

ActionResult Index,

First int? page=1, this for your request, when it will be null, the default page will be first one page=1.

next step is reading data from our Repository.

  var model = repositoryStudent.GetStudent;

if no page was specified in the query string, default to the first page (1)

  var pageNumber = page ?? 1; 

 will only contain 25 products max because of the pageSize

  var onePageOfProducts = products.ToPagedList(pageNumber, 25); 

Don't forget to call X.PagedList .

using X.PagedList;

Now to our HTML code.

Just generate Index view.

next Photo will show you what you will have new change.

Again same what we call in our controller, we should call 2 library from X.PagedList.

@using X.PagedList.Mvc;  
@using X.PagedList;

after that to have a good style you need to add,

<link href="/Content/PagedList.css" rel="stylesheet" type="text/css" />

so after all that the most important part is.

@Html.PagedListPager((IPagedList)Model, page => Url.Action("Index", new { page }))


The result will be same next photo.




Youtube Video.....


Hope this article is helpful.

If you have any question, just write in the comment.