1

I wanted to implement a simple pagination, and PagedList.MVC NuGet package sounded like the best solution for me. HOWEVER, when I click on generated buttons to go to 2nd, 3rd, etc. page, 1st one remains active, and all that happens is refresh of the first page, but I obviously want it to navigate to the expected page... I followed these two tutorials to see if I've done everything right:

Github

Microsoft

My controller:

public ActionResult Index(int? pageNumber)
    {         
            var modelList = _employeeService.GetEmployeeViewToPagedList(pageNumber);
            return View(modelList);
    }

The service method that gets called (I know that "ToPagedList()" is usually called from the controller, but the current state is a result of trying everything, and the fact that I get "DbContext disposed" error if I modify to return something like "View(modelList.ToPagedList(pageNumber, pageSize))" from the controller):

public IPagedList<EmployeeView> GetEmployeeViewToPagedList(int? pageNumber)
    {
        using (var _unitOfWork = UnitOfWork.GetUnitOfWork())
        {
            var list = (IQueryable<EmployeeView>)_unitOfWork.context.EmployeeViews.OrderByDescending(x => x.Id);
            return list.ToPagedList((pageNumber ?? 1), 10);
        }
    }

My view:

@model PagedList.IPagedList<Company.DAL.Views.EmployeeView>
@using PagedList.Mvc;
<link href="~/Content/PagedList.css" rel="stylesheet" type="text/css" />

@{
    ViewBag.Title = "Index";
}

<h2>List of all employees</h2>

<p>
    @Html.ActionLink("Add new employee", "AddNewEmployee")
</p>
@if (Model != null && Model.Count() > 0)
{
    <table class="table">
        ... all needed <tr>'s, <th>'s, <td>'s ...
    </table>
    <br/>
    @Html.PagedListPager(Model, page => Url.Action("Index", new { page, pageSize = 
Model.PageSize }))
}

I am trying to figure this out for days now, and the closest I got was this question, but I am not sure where to find that JS function, so I could try that as well.

EDIT: Generated HTML:

<div class="pagination-container">
    <ul class="pagination">
        <li class="active"><a>1</a></li>
        <li><a href="/Play?page=2&amp;pageSize=3">2</a></li>
        <li><a href="/Play?page=3&amp;pageSize=3">3</a></li>
        <li class="PagedList-skipToNext"><a href="/Play?page=2&amp;pageSize=3" rel="next">»</a></li>
    </ul>
</div>
  • The code you have shown should work fine. What is the actual html that is being generated for your page number links? –  Aug 26 '18 at 02:49
  • @StephenMuecke See the generated HTML above. – Marina_Radiskovic Aug 26 '18 at 02:57
  • Remove the `pageSize = Model.PageSize` from the `@Html.PagedListPager(...)` –  Aug 26 '18 at 02:59
  • @StephenMuecke I wish that worked, but at least my url looks a little better now. – Marina_Radiskovic Aug 26 '18 at 03:07
  • What are you getting now? –  Aug 26 '18 at 03:11
  • @StephenMuecke The same thing, it just keeps refreshing the first page without navigating to the actual set. URL changes on every click though, didn't mention that, but the whole page just looks the same. – Marina_Radiskovic Aug 26 '18 at 03:19
  • have you tried to debug project? what is controller returning actually? the correct set of objects? – AlexiAmni Aug 28 '18 at 12:04
  • 1
    @AlexiAmni I found my mistake, it was a dumb mistake, but it took me more time than I'd like to admit. Index method in my controller accepts an int variable named "pagedNumber", but on my Razor view I pass a variable named "page" from the helper method. They must have the same name! – Marina_Radiskovic Aug 29 '18 at 18:26

1 Answers1

0

I decided to post an answer here, since I solved the problem, and somebody else might find this useful. So, in the controller, my Index method looks like this:

public ActionResult Index(int? pageNumber)
{     
    //some logic
}

As you can see, it accepts an int variable named pageNumber as a parameter. But then there's this on my view:

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

SO, here I am passing a variable named page to my Index method. That's the mistake! Variable in the method parameter list has to be named page as well.