I have multiple classes to be shown in a view so I used a 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; }
}
And here's my controller code (other codes omitted)
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);
Then here's my main view which renders partial view for each entities in tab panes (other codes omitted)
@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>
And here's one of my partial views
@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 }))
However, this returns the following error
The model item passed into the dictionary is of type 'Proj.Models.myViewModel', but this dictionary requires a model item of type 'PagedList.IPagedList`1[Proj.Models.myViewModel]'.