Marina_Radiskovic
Marina_Radiskovic

Reputation: 43

PagedList.MVC - Page buttons(links) don't work as expected

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>

Upvotes: 1

Views: 2437

Answers (1)

Marina_Radiskovic
Marina_Radiskovic

Reputation: 43

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.

Upvotes: 0

Related Questions