使用 asp.net MVC 对搜索结果进行分页
我遇到了一种情况,通过此处的搜索找不到解决方案。场景如下:
我有一个搜索表单,其中包含 2 个必填字段和多个可选字段。该表单发送到一个操作方法,该方法确定选择哪些字段并构建一个 List<> 列表。与搜索条件匹配的对象。然后我传递该 List<>到视图进行显示。
我遇到的这个问题涉及到如何使用 ASP.NET MVC 完成分页。对于过去的项目,我使用了自定义 Html 帮助器来创建包含查询参数以及“页面”参数的链接。然后它使用 GET 请求和 .Take().Skip() 格式。
我在这个项目上遇到了困难,因为我无法使用 GET 请求作为搜索条件,并且我无法找到一种方法来保持 List<>在内存中执行通常的“页面”参数技巧。
我考虑过存储 List<>在会话中,但对象和列表可能非常大。
我认为这是高级搜索表单的一个常见问题,但我似乎找不到好的解决方案。任何帮助将不胜感激。谢谢!
I have a situation that I couldn't find a solution for through my searches on here. Here is the scenario:
I have a search form with 2 required fields and multiple optional ones. The form posts to an action method that determines which fields are selected and builds a List<> of objects that match the search criteria. I then pass that List<> to the view for display.
This issue I am running into involves how paging is typically done with asp.net mvc. For past projects I have used a custom Html helper that creates links which include the query parameters as well as a "page" parameter. It then uses a GET request and the .Take().Skip() format.
I've hit a wall on this project as I can't use a GET request for the search criteria and I can't figure out a way to keep the List<> in memory to do the usual "page" parameter trick.
I thought about storing the List<> in the session but the objects and the list could be very large.
I would think this is a popular issue with advanced search forms but I can't seem to find a good solution. Any help would be appreciated. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如何缓存搜索结果对象并给它一个唯一的键。然后,您可以让分页链接引用该唯一的 (SearchID),并让您的操作查找该对象,将其从缓存中拉出,然后从那里跳过/获取。
这不会为每个请求重建对象,从而使页面加载速度更快并减少数据库/应用程序的压力。
这是一篇关于缓存的文章:
https://web.archive.org/web/20211020111559/https://aspnet.4guysfromrolla.com/articles/100902-1.aspx
这是一个有关缓存的视频:
http://www.asp.net/learn/Videos/video-6206.aspx
注意:确保指定缓存对象的到期日期。
How about cacheing the search result object and giving it a unique key. You would then have your paging links reference that unique (SearchID) and have your action look for that object, pull it from cache and Skip/Take from there.
This will not rebuild the object for every request, making page loading much faster and reducing strain on your database/application.
Here is a article about cacheing:
https://web.archive.org/web/20211020111559/https://aspnet.4guysfromrolla.com/articles/100902-1.aspx
Here is a video about cacheing:
http://www.asp.net/learn/Videos/video-6206.aspx
Note: Be sure you specify expiration date on the cached object.
如果我理解正确的话,您只想加载一次搜索结果,然后逐页浏览它们。
您研究过 jQuery 分页功能吗?您可以将整个列表转储到页面并使用 JavaScript 来处理分页(如果您愿意,还可以进行排序)。
可以在以下位置找到示例: http://beckelman.net/demos/jqueryTableSorterConPaging/default.aspx< /a>
If I understand properly, you only want to load the search results one time, and then page through them.
Have you looked into any jQuery paging functionality? You can just dump the entire list to the page and use JavaScript to handle the paging (and sorting if you like).
An example can be found at http://beckelman.net/demos/jqueryTableSorterConPaging/default.aspx
将所有内容放在相同的形式中:必填字段、可选字段、页面链接。
两种可能性:
page1
、page2
...):这将允许您提交表单时获取所需的页面。因此,单击任何寻呼机链接都会提交表单,其中包含构建列表和寻呼机链接所需的所有数据。
Put everything in the same form: the required fields, the optional fields, the page links.
Two possibilities:
page1
,page2
, ...): this will allow you to get the desired page when the form is submitted.So clicking on any of the pager links will submit the form with all the data you need to build the list and pager links.