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

标签 asp.net-mvc azure pagination

我正在尝试将分页应用于我的 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;

    }

Controller :-

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 个条目。现在我如何显示和实现“上一页”和“下一页”以显示下一页上的其余条目?如何实现 Controller 和 HTML 页面的其余部分?

感谢任何帮助。

最佳答案

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

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

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

要实现下一页类型的功能,请存储表服务返回的继续 token 并在查询中使用它。

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

要实现首页类型的功能,只需发出不带继续 token 的查询即可。

请查看ExecuteQuerySegmented如果要实现分页,请使用存储客户端库中的方法。

更新

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

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();
        }
    }
}

关于asp.net-mvc - 添加分页 MVC 和 Azure 表存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28599511/

相关文章:

asp.net-mvc - 在没有 HttpContext 或 ControllerContext 的情况下将 ASP.NET MVC 字符串渲染到 View ?

c# - Razor 复选框未绑定(bind)到模型

python - 如何设置始终加密的 Azure SQL 数据库并用数据填充它?

ios - Azure 通知中心似乎不适用于 ios 10 和 swift 3

php - 使用 PHP 的分页代码中缺少 Ahref 链接

postgresql - 使用非唯一键的 Postgres 分页?

asp.net-mvc - ASP.NET MVC添加 Controller 已禁用

asp.net-mvc - 为什么我的样式表将我重定向到登录?

Azure 环境分离

cakephp - 如何在 cakephp 中显示下拉分页数据中的选定值?