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

时间:2016-08-03 17:36:44

标签: c# asp.net-mvc entity-framework

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

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

Using a PagedList with a ViewModel ASP.Net MVC

但我有不同的视图模型:

我的实体类

public class Summary
{

}

viewmodel是:

 public class SummaryViewModel
    {
     ...         
     ....
    }

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

       public List<SummaryViewModel> SummaryUnrestricted { get; set; }

    }

我的Controller类:

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在两个表上应用分页?

1 个答案:

答案 0 :(得分:0)

我解决了这个问题。代替列表&lt;&gt;在视图模型中,我用..替换它。

public class DashboardViewModel
    {
       public PagedList<SummaryViewModel> SummaryRestricted { get; set; }

       public PagedList<SummaryViewModel> SummaryUnrestricted { get; set; }

    }
它现在正常工作。