PagedList分页不起作用Mvc4



我收到的这个问题是,当我选择一个类别时,什么都不显示。没有项目没有页码。

遵循了Troy Goodes的PagedList示例,但无法使其工作。jQuery加载三个部分视图(category,items.descript),我正在尝试对项目结果进行分页

这是我的控制器

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);
    }

查看

@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);
                });
            });
        });

为了给你一个详细的答案,我应该看看你收到的问题,因为你没有提到到底出了什么问题。无论如何,我已经在您的代码中看到了一些问题。

  • 您正在调用ViewBag.OnePageOfProducts,但没有在ActionResult中创建此ViewBag。因此修改如下:

    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);
    }
    
  • 您正在使用id参数,但在@Html.PagedListPager帮助程序中忘记了它。若不定义它,PagedList将从第二页开始丢失结果。所以修改它:

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

最新更新