使用 asp.net MVC 对搜索结果进行分页 [英] Paging search results with asp.net MVC

查看:21
本文介绍了使用 asp.net MVC 对搜索结果进行分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一种情况,无法通过我在此处的搜索找到解决方案.这是场景:

I have a situation that I couldn't find a solution for through my searches on here. Here is the scenario:

我有一个包含 2 个必填字段和多个可选字段的搜索表单.该表单发布到一个动作方法,该方法确定选择了哪些字段并构建与搜索条件匹配的对象的 List<>.然后我将该 List<> 传递给视图以进行显示.

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.

我遇到的这个问题涉及通常如何使用 asp.net mvc 完成分页.对于过去的项目,我使用了一个自定义的 Html 帮助程序来创建包含查询参数和页面"参数的链接.然后它使用 GET 请求和 .Take().Skip() 格式.

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.

我在这个项目上遇到了困难,因为我无法对搜索条件使用 GET 请求,而且我无法找到一种方法将 List<> 保留在内存中以执行通常的页面"参数把戏.

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.

我想在会话中存储 List<>,但对象和列表可能非常大.

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!

推荐答案

如何缓存搜索结果对象并给它一个唯一的键.然后,您将让您的分页链接引用该唯一 (SearchID) 并让您的操作查找该对象,将其从缓存中提取并从那里跳过/获取.

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:

http://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.

这篇关于使用 asp.net MVC 对搜索结果进行分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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