ASP.NET MVC分页不适用于ViewModel

时间:2018-07-26 06:33:47

标签: asp.net-mvc pagination pagedlist

我在项目中使用分页。我有搜索框,并将搜索文本发送到操作结果。

我在搜索ActionResult时遇到了断点。在选择查询后,我的新闻和公告就满了。

搜索操作结果

 public ActionResult Search(string searchText,int? page)
    {
     int pageIndex=page??1;
     int dataCount=20;

     dbContext=new dbContext();
     SearchViewModel searchViewModel=new SearchViewModel();

     var news=(from news in dbContext.News
               where news.Title.Contains(searchText)
               select news).ToList();


     var announcement=(from announcement in dbContext.Announcement
               where announcement.Title.Contains(searchText)
               select announcement).ToList();


     searchViewModel.News=news;
     searchViewModel.Announcement=announcement;

     List<SearchViewModel> searchViewModelList=new List<SearchViewModel> {searchViewModel};

     return View(searchViewModelList.ToPagedList(pageIndex,dataCount));

    }

SearchViewModel

public class SearchViewModel
{

   public IEnumerable<News> News {get;set;}
   public IEnumerable<Announcement> Annocument{get;set;}    
}

我的模型在视图中不为空。搜索后,我将显示所有数据标题。 PagedList已显示在视图上,但只有1页。

Search.cshtml

@model PagedList.IPagedList<MyProject.ViewModels.SearchViewModel>
@using PagedList
@using PagedList.Mvc

<div class=container>
@foreach (var item in Model)
{ 
  foreach (var news in item.News)
 {
  @news.Title
 }

 foreach(var announcement in item.Announcement)
 {
  @announcement.Title
 }

}

@Html.PagedListPager(Model,page=>Url.Action("Search",new {page=page}));
</div>

我以前在项目中使用过PagedList。但是我想在ViewModel中使用。我哪里错了?

1 个答案:

答案 0 :(得分:1)

ToPagedList()设计用于数据模型,而不是视图模型。您当前正在做的是通过调用News从数据库返回所有Accouncement.ToList()记录(这违背了使用服务器端分页的全部目的),并将所有这些记录分配给视图模型的一个实例的属性,然后将ToPagedList()应用于单个实例。

您需要将视图模型属性更改为IPagedList<T>,然后将分页应用于每个属性,而不是视图模型本身。您的视图模型应该是

public class SearchViewModel
{
    public string SearchText { get; set; }
    public IPagedList<News> News { get; set; }
    public IPagedList<Announcement> Annoucements { get; set; }
}

,然后控制器方法将是(请注意,您的linq查询需要包含orderby子句,但我不知道您要对哪个属性进行排序)

public ActionResult Search(string searchText, int? page)
{
    int pageIndex = page ?? 1;
    int dataCount = 20;

    dbContext=new dbContext();
    IQueryable<News> news = (from news in dbContext.News 
        where news.Title.Contains(searchText) orderby ?? select news);
    IQueryable<News> announcements = (from announcement in dbContext.Announcement 
        where announcement.Title.Contains(searchText) orderby ?? select announcement);

    SearchViewModel model = new SearchViewModel
    {
        SearchText = searchText,
        News = news.ToPagedList(pageIndex, dataCount)
        Annoucements = announcements.ToPagedList(pageIndex, dataCount)
    };
    return View(model);
}

然后将视图修改为

@model SearchViewModel
@using PagedList
@using PagedList.Mvc

... // your form with the input for SearchText

<div class=container>
    @foreach (var news in Model.News)
    {
        @news.Title
    }
    @foreach(var announcement in Model.Announcements)
    {
        @announcement.Title
    }
    @if (Model.News.Count() > Model.Announcements.Count())
    {
        @Html.PagedListPager(Model.News, page=>Url.Action("Search",new { page = page, searchText = Model.SearchText }));
    }
    else
    {
        @Html.PagedListPager(Model.Announcements, page=>Url.Action("Search",new { page = page, searchText = Model.SearchText }));
    }
</div>

请注意,if块可确保在导航到后续页面时显示页码,在这些页面中您可能有一些News项但没有Announcement项,反之亦然。但是,这不能保证总页数正确(可能有45条匹配的News记录和65条匹配的Announcement记录)