【问题标题】:Issue with Pagination with MVC 4MVC 4 的分页问题
【发布时间】:2014-01-13 13:15:02
【问题描述】:

我正在尝试使用 MVC 实现分页。

我的控制器:

[AcceptVerbs(HttpVerbs.Get)]


        public ActionResult Index(int? page)
        {
            int pageSize = 2;
            int PageNumber = (page ?? 1);
            return PartialView(new UserListModel
            {
                userModel = Repo.GetUsers().OrderBy(x=>x.FullName).ToPagedList(PageNumber, pageSize),
                ddlDept = Repo.GetPairModel("Dept")
            });
        }

我的 Index.cshtml:

    @model PagedList.IPagedList<UserListModel>
    @using PagedList.Mvc;
    ----
     @foreach (var item in Model)
                {
                 -----------
                }
Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount

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

但我遇到以下问题:

    The model item passed into the dictionary is of type 'UserListModel', but this
 dictionary requires a model item of type 'PagedList.IPagedList`1[UserListModel]'.

【问题讨论】:

    标签: asp.net-mvc-4 pagination


    【解决方案1】:

    您需要将 UserListModel 转换为 IPagedList,而不是它的内部 userModel 属性。您的视图需要 IPagedList 而不是 IPagedList

    【讨论】:

    • 如何将 UserListModel 转换为 IPagedList。你能帮忙看看语法吗?
    【解决方案2】:

    您将 UserListModel 返回到期望 IPagedList&lt;UserListModel&gt; 的视图

    改一下

    @model PagedList.IPagedList<UserListModel>
    

    @model UserListModel
    

    【讨论】:

    • 但是我也需要分页。
    【解决方案3】:

    通过以下步骤解决了我的问题:

    型号

     public class UserListModel
        {
            public IEnumerable<PairModel> ddlDept { get; set; }
            public PagedList.IPagedList<UserTbl> userModel { get; set; }
        }
    

    查看

    @model UserListModel
    @using PagedList.Mvc;
    @{
        PagedList.IPagedList<UserTbl> userListModel = Model.userModel;
    }
    

    谢谢

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-17
      • 1970-01-01
      • 2011-05-10
      • 2018-05-14
      • 2020-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多