asp.net-mvc – 添加分页MVC和Azure表存储

前端之家收集整理的这篇文章主要介绍了asp.net-mvc – 添加分页MVC和Azure表存储前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图将分页应用到我的MVC应用程序中.我使用Azure表存储

这是我尝试过的: –

  1. public List<T> GetPagination(string partitionKey,int start,int take)
  2. {
  3. List<T> entities = new List<T>();
  4.  
  5. TableQuery<T> query = new TableQuery<T>().Where(TableQuery.GenerateFilterCondition("PartitionKey",QueryComparisons.Equal,partitionKey.ToLower()));
  6. entities = Table.ExecuteQuery(query).Skip(start).Take(take).ToList();
  7.  
  8. return entities;
  9.  
  10. }

控制器: –

  1. public ActionResult Index()
  2. {
  3.  
  4. key= System.Web.HttpContext.Current.Request[Constants.Key];
  5. if (String.IsNullOrEmpty(key))
  6. return RedirectToAction("NoContext","Error");
  7.  
  8. var items= _StorageHelper.GetPagination(key,3);
  9. ItemCollection itemCollection = new ItemCollection();
  10. itemCollection .Items= Mapper.Map<List<ItemChart>,List<ItemModel>>(items);
  11. itemCollection .Items.ForEach(g => g.Object = g.Object.Replace(key,""));
  12.  
  13.  
  14. return View(itemCollection);
  15. }

这当前给了我数据中的前3个条目.现在,我如何显示和实现“上一页”和“下一页”以显示下一页上的其他条目?如何实现控制器和HTML页面的其余部分?

任何帮助表示赞赏.

解决方法

分页时,需要考虑以下几点:

> Table Service不支持所有LINQ运算符(以及OData查询选项).例如,不支持Skip.有关支持的操作符列表,请参阅此链接https://msdn.microsoft.com/en-us/library/azure/dd135725.aspx.
>分页与Table Service一起使用的方式是,当您查询表以获取某些数据时,表服务可以返回的最大实体数为1000.无法保证始终返回1000个实体.它可能小于1000甚至0,具体取决于您的查询方式.但是,如果有更多可用结果,Table Service将返回称为Continuation Token的内容.您必须使用此标记从表服务中获取下一组结果.有关查询超时和分页的更多信息,请参阅此链接https://msdn.microsoft.com/en-us/library/azure/dd135718.aspx.

考虑到这两个因素,您无法真正实现寻呼解决方案,用户可以直接跳转到特定页面(例如,用户坐在第1页,然后用户无法转到第4页).您最多可以实现下一页,上一页和第一页功能.

要实现下一页功能,请存储表服务返回的延续令牌,并在查询中使用该令牌.

要实现以前页面类型的功能,必须存储在数组或其他内容中返回的所有连续标记,并跟踪用户当前所在的页面(即当前页面索引).当用户想要转到上一页时,您只需获取上一个索引的延续令牌(即当前页面索引 – 1)并在查询中使用它.

要实现第一页类型的功能,只需在没有延续令牌的情况下发出查询.

如果要实现分页,请查看Storage Client Library中的ExecuteQuerySegmented方法.

UPDATE

请参阅下面的示例代码.为简单起见,我只保留了第一页和下一页功能

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Microsoft.WindowsAzure.Storage.Auth;
  7. using Microsoft.WindowsAzure.Storage.Blob;
  8. using Microsoft.WindowsAzure.Storage;
  9. using Microsoft.WindowsAzure.Storage.Queue;
  10. using Microsoft.WindowsAzure.Storage.Table;
  11. namespace TablePaginationSample
  12. {
  13. class Program
  14. {
  15. static string accountName = "";
  16. static string accountKey = "";
  17. static string tableName = "";
  18. static int maxEntitiesToFetch = 10;
  19. static TableContinuationToken token = null;
  20. static void Main(string[] args)
  21. {
  22. var cloudStorageAccount = new CloudStorageAccount(new StorageCredentials(accountName,accountKey),true);
  23. var cloudTableClient = cloudStorageAccount.CreateCloudTableClient();
  24. var table = cloudTableClient.GetTableReference(tableName);
  25. Console.WriteLine("Press \"N\" to go to next page\nPress \"F\" to go first page\nPress any other key to exit program");
  26. var query = new TableQuery().Take(maxEntitiesToFetch);
  27. var continueLoop = true;
  28. do
  29. {
  30. Console.WriteLine("Fetching entities. Please wait....");
  31. Console.WriteLine("-------------------------------------------------------------");
  32. var queryResult = table.ExecuteQuerySegmented(query,token);
  33. token = queryResult.ContinuationToken;
  34. var entities = queryResult.Results;
  35. foreach (var entity in entities)
  36. {
  37. Console.WriteLine(string.Format("PartitionKey = {0}; RowKey = {1}",entity.PartitionKey,entity.RowKey));
  38. }
  39. Console.WriteLine("-------------------------------------------------------------");
  40. if (token == null)//No more token available. We've reached end of table
  41. {
  42. Console.WriteLine("All entities have been fetched. The program will now terminate.");
  43. break;
  44. }
  45. else
  46. {
  47. Console.WriteLine("More entities available. Press \"N\" to go to next page or Press \"F\" to go first page or Press any other key to exit program.");
  48. Console.WriteLine("-------------------------------------------------------------");
  49. var key = Console.ReadKey();
  50. switch(key.KeyChar)
  51. {
  52. case 'N':
  53. case 'n':
  54. continue;
  55. case 'F':
  56. case 'f':
  57. token = null;
  58. continue;
  59. default:
  60. continueLoop = false;
  61. break;
  62. }
  63. }
  64. } while (continueLoop);
  65. Console.WriteLine("Press any key to terminate the application.");
  66. Console.ReadLine();
  67. }
  68. }
  69. }

猜你在找的asp.Net相关文章