我正在尝试用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]'.
答案 0 :(得分:0)
您需要将UserListModel转换为IPagedList,而不是它的内部userModel属性。您的视图期望IPagedList不是IPagedList
答案 1 :(得分:0)
您要将UserListModel
返回到期待IPagedList<UserListModel>
更改此
@model PagedList.IPagedList<UserListModel>
到
@model UserListModel
答案 2 :(得分:0)
通过以下步骤解决了我的问题:
模型
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;
}
由于