使用PagedList向ASP MVC3网站添加分页功能

时间:2012-12-18 19:36:54

标签: asp.net-mvc asp.net-mvc-3 pagedlist

我们网站的搜索功能之一为一个页面返回的结果太多了,所以我试图添加这里提供的分页功能:https://github.com/TroyGoode/PagedList

解决方案构建正确并且页面也会加载,但是当我尝试进行搜索时,页面的控制器/ Index()方法会抛出“NotSupportedException”:

The method 'Skip' is only supported for sorted input in LINQ to Entities. The method 'OrderBy' must be called before the method 'Skip'.

当抛出此异常时,Visual Studio 2010指向return语句。这只是我在ASP MVC工作的第二天,所以欢迎提出任何建议。谢谢!

            case "name":
                //if no comma, do a combined search  by last name and by corporate name.
                searchString = searchString.ToUpper();

                var lastAgents =
                    db.Agent.OrderBy(s => s.LastName).Where(s => s.LastName.ToUpper().StartsWith(searchString)).Include(
                        a => a.AgentIdentification).Include(a => a.SymetraNumberToAgentId);
                //end new code
                var corp2Agents =
                    db.Agent.OrderBy(s => s.CorporateName).Where(s => s.CorporateName.ToUpper().StartsWith(searchString)).Include(
                        a => a.AgentIdentification);
                if ((corp2Agents.Count() == 0) & (lastAgents.Count() == 0)) ViewBag.ErrorMessage = "None found in search for Last Names and Companies beginning with " + search1;
                else ViewBag.Message = "Results of Last Name and Company Name search.  Found " + (corp2Agents.Count() + lastAgents.Count()).ToString();

                pageNumber = (page ?? 1);
                return View(lastAgents.Union(corp2Agents).ToPagedList(pageNumber, pageSize));

2 个答案:

答案 0 :(得分:0)

永远但我找到了答案。这两个陈述

                var lastAgents =
                    db.Agent.OrderBy(s => s.LastName).Where(s => s.LastName.ToUpper().StartsWith(searchString)).Include(
                        a => a.AgentIdentification).Include(a => a.SymetraNumberToAgentId);
                //end new code
                var corp2Agents =
                    db.Agent.OrderBy(s => s.CorporateName).Where(s => s.CorporateName.ToUpper().StartsWith(searchString)).Include(
                        a => a.AgentIdentification);

包含OrderBy,但在Union语句中也是如此。最终的“返回”声明如下:

return View((lastAgents.Union(corp2Agents)).OrderBy(s => s.sNumber).ToPagedList(pageNumber, pageSize));

答案 1 :(得分:0)

尝试在控制器中添加.OrderBy(s => s.sNumber),如下所示:

var lastAgents =
                    db.Agent.Where(s => s.LastName.ToUpper().StartsWith(searchString)).Include(
                        a => a.AgentIdentification).Include(a => a.SymetraNumberToAgentId).OrderBy(s => s.sNumber);
                //end new code
                var corp2Agents =
                    db.Agent.Where(s => s.CorporateName.ToUpper().StartsWith(searchString)).Include(
                        a => a.AgentIdentification).OrderBy(s => s.CorporateName);