Asp.net-Mvc

添加 Pagination MVC 和 Azure 表儲存

  • February 19, 2015

我正在嘗試將分頁應用於我的 MVC 應用程序。我正在使用 Azure 表儲存

這是我嘗試過的: -

public List<T> GetPagination(string partitionKey, int start, int take)
   {
       List<T> entities = new List<T>();

       TableQuery<T> query = new TableQuery<T>().Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, partitionKey.ToLower()));
       entities = Table.ExecuteQuery(query).Skip(start).Take(take).ToList();

       return entities;

   }

控制器:-

public ActionResult Index()
       {

           key= System.Web.HttpContext.Current.Request[Constants.Key];
           if (String.IsNullOrEmpty(key))
               return RedirectToAction("NoContext", "Error");

           var items= _StorageHelper.GetPagination(key,0,3);
          ItemCollection itemCollection = new ItemCollection();
           itemCollection .Items= Mapper.Map<List<ItemChart>, List<ItemModel>>(items);
           itemCollection .Items.ForEach(g => g.Object = g.Object.Replace(key, ""));


           return View(itemCollection);
       }

這目前給了我數據中的前 3 個條目。現在如何顯示和實現“上一個”和“下一個”以在下一頁顯示其餘條目?如何實現控制器和 HTML 頁面的其餘部分?

任何幫助表示讚賞。

說到分頁,有幾點需要考慮:

  • 並非所有 LINQ 運算符(以及 OData 查詢選項)都支持表服務。例如Skip不支持。有關支持的運算符列表,請參閱此連結:https ://msdn.microsoft.com/en-us/library/azure/dd135725.aspx 。
  • 分頁與表服務一起工作的方式是,當您查詢表以獲取一些數據時,表服務可以返回的最大實體數為 1000。不能保證總是返回 1000 個實體。根據您的查詢方式,它可能小於 1000 甚至 0。但是,如果有更多可用結果,表服務會返回一個名為 a 的內容Continuation Token。您必須使用此令牌從表服務中獲取下一組結果。有關查詢超時和分頁的更多資訊,請參閱此連結:https ://msdn.microsoft.com/en-us/library/azure/dd135718.aspx 。

考慮到這兩個因素,您無法真正實現使用者可以直接跳轉到特定頁面的分頁解決方案(例如,使用者坐在第 1 頁上,然後使用者無法轉到第 4 頁)。最多可以實現下一頁、上一頁和首頁的功能。

要實現next page某種功能,請儲存表服務返回的延續令牌並在查詢中使用它。

要實現previous page某種功能,您必須將返回的所有延續令牌儲存在數組或其他東西中,並跟踪使用者目前所在的頁面(即目前頁面索引)。當使用者想要轉到上一頁時,您只需獲取上一個索引的延續標記(即目前頁面索引 - 1)並在查詢中使用它。

要實現first page某種功能,只需發出不帶延續令牌的查詢。

ExecuteQuerySegmented如果要實現分頁,請查看 Storage Client Library 中的方法。

更新

請參閱下面的範常式式碼。為了簡單起見,我只保留了首頁和下一頁功能:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.WindowsAzure.Storage.Auth;
using Microsoft.WindowsAzure.Storage.Blob;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Queue;
using Microsoft.WindowsAzure.Storage.Table;
namespace TablePaginationSample
{
   class Program
   {
       static string accountName = "";
       static string accountKey = "";
       static string tableName = "";
       static int maxEntitiesToFetch = 10;
       static TableContinuationToken token = null;
       static void Main(string[] args)
       {
           var cloudStorageAccount = new CloudStorageAccount(new StorageCredentials(accountName, accountKey), true);
           var cloudTableClient = cloudStorageAccount.CreateCloudTableClient();
           var table = cloudTableClient.GetTableReference(tableName);
           Console.WriteLine("Press \"N\" to go to next page\nPress \"F\" to go first page\nPress any other key to exit program");
           var query = new TableQuery().Take(maxEntitiesToFetch);
           var continueLoop = true;
           do
           {
               Console.WriteLine("Fetching entities. Please wait....");
               Console.WriteLine("-------------------------------------------------------------");
               var queryResult = table.ExecuteQuerySegmented(query, token);
               token = queryResult.ContinuationToken;
               var entities = queryResult.Results;
               foreach (var entity in entities)
               {
                   Console.WriteLine(string.Format("PartitionKey = {0}; RowKey = {1}", entity.PartitionKey, entity.RowKey));
               }
               Console.WriteLine("-------------------------------------------------------------");
               if (token == null)//No more token available. We've reached end of table
               {
                   Console.WriteLine("All entities have been fetched. The program will now terminate.");
                   break;
               }
               else
               {
                   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.");
                   Console.WriteLine("-------------------------------------------------------------");
                   var key = Console.ReadKey();
                   switch(key.KeyChar)
                   {
                       case 'N':
                       case 'n':
                           continue;
                       case 'F':
                       case 'f':
                           token = null;
                           continue;
                       default:
                           continueLoop = false;
                           break;
                   }
               }
           } while (continueLoop);
           Console.WriteLine("Press any key to terminate the application.");
           Console.ReadLine();
       }
   }
}

引用自:https://stackoverflow.com/questions/28599511