MVC实体框架服务器端分页PageList



对于服务器端分页,我使用PageList。我看到:https://www.youtube.com/watch?v=5omEuuIIFcg

我正在使用ViewModel。我按照"Dennis R"给出的步骤。

将PagedList与ViewModel ASP。Net MVC

但我有不同的观点模型:

我的实体类

public class Summary
{
}

视图模型为:

 public class SummaryViewModel
    {
     ...         
     ....
    }
 public class DashboardViewModel
    {
       public List<SummaryViewModel> SummaryRestricted { get; set; }
       public List<SummaryViewModel> SummaryUnrestricted { get; set; }
    }

我的控制器类:

public ActionResult Display(int page = 1, int pagesize = 4)
        {            
            var entitySummaries = _dbContext.Summaries.ToList();
            var vm = MapEntityToViewModel(entitySummaries );
            //return View(vm);
//return View(vm.FundsUnrestricted.ToPagedList(page, pagesize)); ????
        }
        DashboardViewModel MapEntityToViewModel(List<Summary> funds)
        {
            DashboardViewModel dashboardViewModel = new DashboardViewModel();
            List<Summary> unRestricted =  funds.Where(x => xxx).ToList() ;
            List<Summary> restricted = funds.Where(x => xx).ToList();
            dashboardViewModel.SummaryUnrestricted = unRestricted.Select(x => new SummaryViewModel(x)).ToList(); 
            dashboardViewModel.SummaryRestricted = restricted.Select(x => new SummaryViewModel(x)).ToList();
            return dashboardViewModel;           
        }

我的观点是:

@model PagedList.IPagedList<ViewModels.DashboardViewModel> 
@using PagedList.Mvc;
@using PagedList;
<table id="Restricted" class="table table-bordered">
   @foreach (ViewModels.SummaryViewModel item in Model.SummaryRestricted)
    {
       <tr> <tr>
    }
</table>
<table id="UnRestricted" class="table table-bordered">
   @foreach (ViewModels.SummaryViewModel item in Model.SummaryUnrestricted )
    {
       <tr> <tr>
    }
</table>

我的视图必须在同一页面上同时显示限制摘要和非限制摘要的表。有人能帮我如何使用pageList对这两个表进行分页吗?

我解决了这个问题。代替列表<>在视图模型中,我将其替换为。。
public class DashboardViewModel
    {
       public PagedList<SummaryViewModel> SummaryRestricted { get; set; }
       public PagedList<SummaryViewModel> SummaryUnrestricted { get; set; }
    }

它现在运行良好。

最新更新