我有多个类要在视图中显示,所以我使用了viewmodel
myViewModel
public class myViewModel
{
public PagedList.IPagedList<entities1> entities_one { get; set; }
public PagedList.IPagedList<entities2> entities_two { get; set; }
public PagedList.IPagedList<entities3> entities_three { get; set; }
}
这是我的控制器代码(其他代码省略)
myViewModel model = new myViewModel()
{
entities_one = db.mydatabase.Where(a => a.id == query).OrderBy(a => a.col1).ToPagedList(pageNumber, pageSize),
entities_two = db.mydatabase.Where(a => a.id == query).OrderBy(a => a.col1).ToPagedList(pageNumber, pageSize),
entities_three = db.mydatabase.Where(a => a.id == query).OrderBy(a => a.col1).ToPagedList(pageNumber, pageSize)
}
return View(model);
然后这是我的主视图,它为选项卡窗格中的每个实体呈现部分视图(省略其他代码)
@model PagedList.IPagedList<Proj.Models.myViewModel>
<div class="tab-content col-xs-12">
<div class="tab-pane fade in active">
@if (ViewBag.Successful == "true")
{
@Html.Partial("_pv1", Model)
}
else
{
<h4 style="padding:5px;">No records found.</h4>
}
</div>
<div class="tab-pane fade in">
@if (ViewBag.Successful == "true")
{
@Html.Partial("_pv2", Model)
}
else
{
<h4 style="padding:5px;">No records found.</h4>
}
</div>
<div class="tab-pane fade">
@if (ViewBag.Successful == "true")
{
@Html.Partial("_pv3", Model)
}
else
{
<h4 style="padding:5px;">No records found.</h4>
}
</div>
这是我的部分观点之一
@model PagedList.IPagedList<Proj.Models.myViewModel>
@using PagedList.Mvc;
@using PagedList;
<ul class="list-group row">
@foreach (var item in Model)
{
<li class="list-group-item col-xs-12">
<h6>@Html.DisplayFor(model => item.entities_one.Select(a => a.col3))</h6>
</li>
}
Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount
@Html.PagedListPager(Model, page => Url.Action("Index", new { page, filter = ViewBag.Filter }))
但是,这会返回以下错误
传递到字典中的模型项的类型为'Proj.Models.myViewModel',但此字典需要类型为'PagedList.IPagedList`1 [Proj.Models.myViewModel]'的模型项。