X.PagedList,手动分页和预先存在的数据 webapi 一次仅前进 1 条记录

问题描述 投票:0回答:1

在 .netcore 中针对已存在的 webapi 设置 PagedList。

webapi 允许我使用 2 个参数(一个 FROM 和一个 TAKE)调用数据。 我已经实现了 X.PagedList,它似乎可以工作,除了它/我如何计算实际页面..

使用X.PagedList处的代码,我实现了手动分页。 问题是,当我单击页码时,我的 TAKE 仅获取 1 条新记录,而不是 NEXT 10 条记录。

第一次加载时,我的 api 调用如下所示

/api/v1/机构?from=0&take=10

第 2 页的调用看起来像

/api/v1/机构?from=1&take=10

显然在这种情况下1应该是11

PagedResults 类

public class PagedResults<T> : List<T>
{
    public List<T> Results { get; set; }
    public string Search { get; set; }
    public bool Empty { get; set; }
}

我的控制器

public IActionResult PagedList(int? page)
    {
        ViewData["Title"] = Title;
        ViewData["PageTitle"] = Title + " List";

        var pageIndex = (page ?? 1) - 1;
        var pageSize = 10;

        //Perform API Call
        var response = GetList(pageIndex, pageSize);

        //Returns List<Institution>
        var Institutions = response.Data;

        //Returns 200, which is the total from the Headers
        string total = response.Headers.Where(x => x.Name == "total").Select(x => x.Value).FirstOrDefault().ToString();

        var PagedList = new StaticPagedList<Institution>(Institutions, pageIndex + 1, pageSize, Convert.ToInt32(total));

        var model = new InstitutionViewModel
        {
            CurrentUser = CurrentUser //From BaseController
        };

        string view = string.Format("~/views/Portal/{0}/List.cshtml", Title);

        ViewData["PagedList"] = PagedList;

        return View(view, model);
    }

获取列表()

#region Helpers

public static IRestResponse<List<Models.Institution>> GetList(int from, int take)
{
    //this create /api/v1/institutions?from=0&Take=10
    string ActionPath = string.Format("Institutions");

    var client = new RestClient(Connect.url);
    var request = new RestRequest(ActionPath, Method.GET);
    request.AddParameter("from", from, ParameterType.QueryString);
    request.AddParameter("take", take, ParameterType.QueryString);

    var result = client.Execute<List<Models.Institution>>(request);

    return result;
}

#endregion

我的看法

@using X.PagedList.Mvc.Core; 
@using X.PagedList; 

@foreach (var item in ViewBag.PagedList)
{
    <tr>
        <td>@item.id</td>
        <td>@item.name</td>
    </tr>
}

 @Html.PagedListPager((IPagedList)ViewBag.PagedList, page => Url.Action("PagedList", new { page }))
asp.net-core pagination pagedlist
1个回答
1
投票

这是一个简单的解决方法,如下所示:

1.查看:

@{
    ViewBag.Title = "Product Listing";
    var pagedList = (IPagedList)ViewBag.PageList;
}
@using X.PagedList.Mvc.Core; @*import this so we get our HTML Helper*@
@using X.PagedList; @*import this so we can cast our list to IPagedList (only necessary because ViewBag is dynamic)*@
@using X.PagedList.Mvc.Common
@using X.PagedList.Mvc.Core.Fluent
<ul>
    @foreach (var name in ViewBag.PageList)
    {
        <li>@name</li>
    }
</ul>
@Html.PagedListPager(pagedList, page => Url.Action("Index", new { page }))

2.控制器:

[HttpGet]
    public IActionResult Index(int page = 1)
    {
        ViewBag.PageList = GetPagedNames(page);
        return View();
    }

    protected IPagedList<string> GetPagedNames(int? page)
    {
        // return a 404 if user browses to before the first page
        if (page.HasValue && page < 1)
            return null;
        // retrieve list from database/wherever
        var listUnpaged = new List<string> { "a", "b", "c", "d", "e", "aa", "bb", "cc", "dd", "ee", "aaa", "bbb", "ccc" ,"ddd","eee","fff","ggg","1","s","f","sd","dsfds"};
        // page the list
        const int pageSize = 10;
        var listPaged = listUnpaged.ToPagedList(page ?? 1, pageSize);
        // return a 404 if user browses to pages beyond last page. special case first page if no items exist
        if (listPaged.PageNumber != 1 && page.HasValue && page > listPaged.PageCount)
            return null;
        return listPaged;
    }
© www.soinside.com 2019 - 2024. All rights reserved.