ASP.NET MVC实践 对查询后分页处理的解决方案

发表于:2009-11-20 10:44

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:未知    来源:51Testing软件测试网采编

分享:

  下面我们来改造一下CreateQueryString方法:

  CreateQueryString

  1  private string CreateQueryString(NameValueCollection values)
  2         {
  3             var builder = new StringBuilder();
  4             if (_searchValues != null)
  5             {
  6                 builder = GetSearchPage(values);
  7             }
  8             else
  9             {
  10                 foreach (string key in values.Keys)
  11                 {
  12                     if (key == _pageQueryName)
  13                     //Don't re-add any existing 'page' variable to the querystring - this will be handled in CreatePageLink.
  14                     {
  15                         continue;
  16                     }
  17
  18                     foreach (var value in values.GetValues(key))
  19                     {
  20                         builder.AppendFormat("&{0}={1}", key, HttpUtility.UrlEncode(value));
  21                     }
  22                 }
  23             }
  24
  25
  26             return builder.ToString();
  27         }
  28         /// <summary>
  29         /// 根据传入的_searchValues来组织分页字符串
  30         /// </summary>
  31         /// <param name="values"></param>
  32         /// <returns></returns>
  33         private StringBuilder GetSearchPage(NameValueCollection values)
  34         {
  35             var builder = new StringBuilder();
  36             Dictionary<string, string> dictionary = new Dictionary<string, string>();
  37             foreach (var item in _searchValues)
  38             {
  39                 dictionary.Add(item.Key, item.Value);
  40             }
  41             foreach (string key in values.Keys)
  42             {
  43                 if (key == _pageQueryName)
  44                 //Don't re-add any existing 'page' variable to the querystring - this will be handled in CreatePageLink.
  45                 {
  46                     continue;
  47                 }
  48
  49                 foreach (var value in values.GetValues(key))
  50                 {
  51                     if (_searchValues.Keys.Contains(key))
  52                     {
  53                         builder.AppendFormat("&amp;{0}={1}", key, HttpUtility.UrlEncode(dictionary[key]));
  54                         dictionary.Remove(key);
  55                     }
  56                     else
  57                     {
  58                         builder.AppendFormat("&amp;{0}={1}", key, HttpUtility.UrlEncode(value));
  59                     }
  60
  61                 }
  62
  63             }
  64             foreach (var item in dictionary)
  65             {
  66                 builder.AppendFormat("&amp;{0}={1}", item.Key, HttpUtility.UrlEncode(item.Value));
  67
  68             }
  69             return builder;
  70         }

  这里边主要添加的就是这个GetSearchPage方法,这个方法就是根据客户端传入的查询条件来组织显示分页字符串的。

  五、缺点:

  因为时使用附加url的方式实现的,所以对于查询条件过多的时候可能有问题,有空我会再实现一个post方案供大家参考。另外这里大家还需要注意的就是url中的查询字符串的名字不能重复,如果重复会直接被替换,详见源码。

  六、源码

44/4<1234
精选软件测试好文,快来阅读吧~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号