rogerthat
rogerthat

Reputation: 1815

PagedList pagination not working Mvc4

This issue I'm receiving is nothing is displayed when I choose a category. No items no pagination.

Followed Troy Goodes example with PagedList but can't get it working. The jQuery loads the three partial views (category, items. descrip) and I'm trying to paginate the item results

This is my controller

public ActionResult Partial( int id, int? page) 
    {

        var Uid = WebSecurity.GetUserId(User.Identity.Name);
        var pageNumber = page ?? 1;

        var query = from i in db.Items
                    where i.user_id == Uid && i.catId == id
                    select i;
        var results = query;
        var onePageOfProducts = results.ToPagedList(pageNumber, 10);

         return PartialView("_Partial1", onePageOfProducts);

    }

View

@using IPagedList;
@using PagedList.Mvc;

@*@foreach (var item in Model)
{*@


@foreach(var item in ViewBag.OnePageOfProducts){



    <tr data-id="@item.ID">
        <td data-id="@item.ID">@item.item_name</td>
        <td></td>
    </tr>

 }
 @Html.PagedListPager( (IPagedList)ViewBag.OnePageOfProducts, 
  page => Url.Action("/Item/Partial", new { page }) )

jQuery

    $('#categories').load("/Category/GetCats", function () {

            $(this).on("click","tr", function () {
                var requestpage = "/Item/Partial?id=" + $(this).data("id");
                //alert(requestpage);   //debug

                $.get(requestpage, function (result) {
                    $("#results").html(result);

                });
            });
        });

        $('#results').load("/Item/Partial", function () {
            var buttons =  
    $("<button class=\"removeBtn\">Remove</button>
    <button class=\"removeBtn\">Edit</button>");

            $(this).on("click", "tr", function () {
                var requestpage = "/Item/FullInfoPartial?id=" + $(this).data("id");
                buttons.appendTo(this);
                //alert(requestpage);  //debug
                $.get(requestpage, function (result) {
                    $("#descrip").html(result);
                });
            });
        });

Upvotes: 0

Views: 1715

Answers (1)

Andrey Gubal
Andrey Gubal

Reputation: 3479

To give you a detailed answer I should see the issue you receive, because you didn't mention what exactly goes wrong. Any way, I already see some problems in your code.

  • You are calling ViewBag.OnePageOfProducts, but you didn't create this ViewBag in your ActionResult. So modify it as follows:

    public ActionResult Partial( int id, int? page) 
    {
    
        var Uid = WebSecurity.GetUserId(User.Identity.Name);
        var pageNumber = page ?? 1;
    
        var query = from i in db.Items
                    where i.user_id == Uid && i.catId == id
                    select i;
        var results = query;
        var onePageOfProducts = results.ToPagedList(pageNumber, 10);
    
         ViewBag.OnePageOfProducts = onePageOfProducts; //* You've fogotten to add this line
    
         return PartialView("_Partial1", onePageOfProducts);
    
    }
    
  • You are using id parameter, but you have forgotten about it in your @Html.PagedListPager helper. If you don't define it PagedList will lose results starting from second page. So modify it:

    @Html.PagedListPager( (IPagedList)ViewBag.OnePageOfProducts, 
    page => Url.Action("/Item/Partial", new { page = page, id = item.ID})
    

Upvotes: 1

Related Questions