Search code examples
c#asp.net-mvcpaginationpagedlist

MVC PagedList sending weird requests to server


I am using MVC with PagedList in order to have a big table divided into multiple pages.

Now, in the web browser, this is what I see: http://localhost:49370/Home/Pending?page=2

Which makes sense. However, when sending a request to the server, this is what the server receives: http://localhost:49370/Home/WhereAmI?_=1429091783507

This is a huge mess, and in turn it makes it impossible to redirect the user to specific pages in the list because I don't know what is the page the user is currently viewing !

Controller code:

public ActionResult Pending(int? page)
        {
            //I have a ViewModel, which is MaterialRequestModel
            IEnumerable<MaterialRequestModel> model = DB.GATE_MaterialRequest
                .Select(req => new MaterialRequestModel(req))
                .ToList();

            int pageNum = page ?? 1;
            return View(model.ToPagedList(pageNum, ENTRIES_PER_PAGE));
        }

View code:

@model IEnumerable<MaterialRequestModel>

<table>
    //table stfuff
</table>

<div style="display: block;text-align: center">
    @Html.PagedListPager((PagedList.IPagedList<MaterialRequestModel>)Model, page => Url.Action("Pending", new { page }), PagedListRenderOptions.ClassicPlusFirstAndLast)
</div>

Is this a limitation of MVC PagedList? Or am I missing something?


Solution

  • It happens that PagedList does not send this type of information to the server. Instead, if you want to know which page is being looked at, you have to use a custom model that has that information, and if you want to make a request usign ajax (the original objective here) you must using a special option:

        @Html.PagedListPager(Model.requests, page => Url.Action("SearchTable", "Home",
            new
            {
                employeesQuery = Model.query.employeesQuery, //your query here
                pageNum = page
            }
                                 ), PagedListRenderOptions.EnableUnobtrusiveAjaxReplacing(new AjaxOptions() { HttpMethod = "GET", UpdateTargetId = "tableAndPaginationDiv", OnComplete = "initiatePendingTableDisplay" }))
    

    Admittedly this solution is poor at best. You can only have 1 option of the entire list (so if you are already using other options somewhere else you can forget it) and you have no control whatsoever on the request made, so customization is really not an option here unless you feel like hooking the calls.

    Anyway, this is how I fixed it, hopefully it will help someone in the future!