How to use PagedList on search results page in MVC?

爷,独闯天下 提交于 2019-12-13 02:25:02

问题


I am using PagedList on my index view which works fine because it is just a list. So when I click on the next page button, it runs against index and pulls the next set of items. My issue is when trying to use this on my results page after an advanced search. My search works fine in returning results. There are 50+ parameters used in the query and some are include or exclude based on what is checked/selected etc. Here is an example of a possible query:

SQLstatement = string.Format("select * from Cards where {0} and {1} and (({2}) or ({3})) 
and CardID In (Select CardID from CardAbilities where {4});", final, finalexcludefrommulti, 
finalsplit, finalmulti, finalability);
var CardList = db.Cards.SqlQuery(SQLstatement).ToList();
var pageNumber = page ?? 1;
            int pageSize = 5;
            ViewBag.OnePageOfCards = pageNumber;
            return View(CardList.ToPagedList(pageNumber, pageSize));

This could result in a SQL Query like this:

select * from Cards where maintypeid = 1 and redbluemanacost is null 
and redblackmanacost is null and blueblackmanacost is null 
and redmanacost is null and bluemanacost is null and blackmanacost is null 
and ((whitegreenmanacost > 0  or redgreenmanacost > 0 or greenbluemanacost > 0  
or greenblackmanacost > 0 or greenorlifecost > 0  or whitegreenmanacost > 0  
or redwhitemanacost > 0 or whitebluemanacost > 0 or whiteblackmanacost > 0  
or whiteorlifecost > 0 ) or (greenmanacost > 0  or whitemanacost > 0 )) 
and CardID In (Select CardID from CardAbilities 
where abilityid = 3 or abilityid = 1007);

Here is part of my view:

 @Html.PagedListPager(Model, page => Url.Action("Results",  new { page }))

When I use the PagedList, the results still come back, but when I click on next page it tries to rerun the query by going to Url.Action "Results" without any values and crashes. Can I store the results and have paging from that? Is there another paging option that would work better? Thanks for any advice. :)


回答1:


I got this to work by creating a session that holds the string for the SQLstatement. On my Search/Index controller, I set the Session to null so that a user can start a new search.

Last part of Search Controller:

Session["PageList"] = SQLstatement;
var CardList = db.Cards.SqlQuery(SQLstatement).ToList();
return View(CardList.ToPagedList(pageNumber, pageSize));

New section added to beginning of Search Controller:

var pageNumber = page ?? 1;
            int pageSize = 5;
            ViewBag.OnePageOfCards = pageNumber;
            if (Session["PageList"] != null)
            {
                string SearchResult = Session["PageList"].ToString();
                var ResultList = db.Cards.SqlQuery(SearchResult).ToList();

                foreach (var item in ResultList)
                {
                    item.SubType = db.SubTypes.Single(x => x.SubTypeID == item.SubTypeID);
                    item.MainType = db.MainTypes.Single(x => x.MainTypeID == item.MainTypeID);
                    item.CardSet = db.CardSets.Single(x => x.CardSetID == item.CardSetID);
                    item.Rarity = db.Rarities.Single(x => x.RarityID == item.RarityID);

                }

                ViewBag.OnePageOfCards = pageNumber;
                return View(ResultList.ToPagedList(pageNumber, pageSize));

Also pass int? page in the parameters for the Search.

In the Search Index Controller added:

Session["PageList"] = null;


来源:https://stackoverflow.com/questions/32014212/how-to-use-pagedlist-on-search-results-page-in-mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!