【问题标题】:PagedList pagination not working Mvc4PagedList分页不起作用Mvc4
【发布时间】:2013-08-18 07:38:11
【问题描述】:

我收到的这个问题是当我选择一个类别时没有显示任何内容。没有项目没有分页。

使用 PagedList 遵循 Troy Goodes 示例,但无法使其正常工作。 jQuery 加载三个部分视图(类别、项目。描述),我正在尝试对项目结果进行分页

这是我的控制器

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

【问题讨论】:

    标签: jquery asp.net-mvc-4 pagination


    【解决方案1】:

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

    • 您正在调用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})
      

    【讨论】:

    • 问题是当我点击一个类别来调出项目时没有任何反应。
    • new { page = page, id = item.ID}) 项目在当前上下文中不存在。如何定义?
    • @healix 这只是一个例子。在您的模型中,它是 catId。所以写:new { page = page, id = item.catId})
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多