如何在MVC中的参数化列表上进行分页?

时间:2018-06-09 10:05:20

标签: asp.net-mvc parameters paging

我想在参数Category的产品列表中进行分页,我使用PagedList mvc 控制器样本

   public ActionResult ListProduct(int id, int? pagePos)
        {
            var list = db.List_Product.Where(e => e.CategoryID == id);
            int pageNumber = (pagePos ?? 1);
            return View(list.ToList().ToPagedList(pageNumber, 2));
        }

并查看ListProduct.cshtml

@*@model IEnumerable<Sales.Areas.Users.Models.List_Product>*@
    @model PagedList.IPagedList<sales.areas.users.models.list_product> @*Maybe error in line here*@
    @using PagedList.Mvc

    <table class="table">
    <tr>
        <th>
            Name
        </th>
        <th>
            ID
        </th>

        <th></th>
    </tr>

    @foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Name)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.ID)
        </td>
    </tr>
    }   
    </table>

    Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount

    @Html.PagedListPager(Model, pagePos => Url.Action("ListProduct", new { pagePos }))

虽然我之前已添加了引用CS0234: The type or namespace name 'IPagedLis' does not exist in the namespace 'PagedList' (are you missing an assembly reference?),但它无法正常运行并出现此错误PagedList

2 个答案:

答案 0 :(得分:0)

没有关于错误的更多信息很难找到罪魁祸首。您应该在web.config

中禁用自定义错误并激活调试

首先检查ActionResult ListProduct中的变量id是否已传递。

然后在您的视图中更改:

@model PagedList.IPagedList<sales.areas.users.models.list_product> 

使用:

@model PagedList.IPagedList<Sales.Areas.Users.Models.List_Product> 

答案 1 :(得分:0)

确保在使用该命名空间中的任何类型

之前,所有必需的命名空间都已到来
@using PagedList; //import this so we can cast our list to IPagedList (only necessary because ViewBag is dynamic)
@using PagedList.Mvc; //import this so we get our HTML Helper 

@model IPagedList<Sales.Areas.Users.Models.List_Product>