如何在MVC的搜索结果页面上使用PagedList? [英] How to use PagedList on search results page in MVC?

查看:173
本文介绍了如何在MVC的搜索结果页面上使用PagedList?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在索引视图上使用PagedList,它工作正常,因为它只是一个列表.因此,当我单击下一页按钮时,它将针对索引运行并拉出下一组项目.我的问题是在高级搜索后尝试在我的结果页面上使用它时.我的搜索可以很好地返回结果.查询中使用了50多个参数,其中某些参数根据检查/选择的内容被包括或排除在外.这是一个可能的查询的示例:

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));

这可能会导致这样的SQL查询:

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);

这是我观点的一部分:

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

当我使用PagedList时,结果仍然会返回,但是当我单击下一页时,它将尝试通过转到Url.Action结果"来重新运行查询,而没有任何值并且崩溃.我可以存储结果并从中进行分页吗?是否有另一个更好的分页选项?感谢您的任何建议. :)

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. :)

推荐答案

我通过创建一个保存SQLstatement字符串的会话来使其工作.在我的搜索/索引"控制器上,将会话"设置为null,以便用户可以开始新的搜索.

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));

还要在搜索参数中传递int? page.

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

在添加的搜索索引控制器中:

In the Search Index Controller added:

Session["PageList"] = null;

这篇关于如何在MVC的搜索结果页面上使用PagedList?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆