分页.net MVC-无需从WebService下载所有记录 [英] Paging .net MVC - without download all records from WebService

查看:78
本文介绍了分页.net MVC-无需从WebService下载所有记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下问题.我想进行漂亮的分页,也不想将所有记录从WebService下载到我的应用程序.

I have a following problem. I want to do nice paging and I do not want to download all records from WebService to my application.

控制器代码:

public ActionResult Vacations(int? page)
        {
            int pageNumber = page ?? 1;
            int quantity = 5;
            using (MyWebService.MyWebServiceClient client = new MyWebService.MyWebServiceClient())
            {
                var vacations = client.GetUserAbsence(SessionManager.CurrentToken, quantity, pageNumber); 
                var onePageOfVacations = vacations.ToPagedList(pageNumber, quantity);
                ViewBag.OnePageOfVacations = onePageOfVacations;
            }
            return View();
        }

查看代码:

<h2>Vacations</h2>
<div>
    <table class="table tablesorter table-striped">
        <thead>
            <tr>
                <th>Data od</th>
                <th>Data do</th>
                <th>Ilość dni</th>
            </tr>
        </thead>
        <tbody>
            @{
                foreach (var v in ViewBag.OnePageOfVacations)
                {
                    <tr>
                        <td>@v.From_Date</td>
                        <td>@v.To_Date</td>
                        <td>@v.Element_Name</td>
                    </tr>
                }
            }
        </tbody>
        <tfoot>
            <tr>
                <th>Data od</th>
                <th>Data do</th>
                <th>Ilość dni</th>
            </tr>
        </tfoot>
    </table>
</div>

<!-- output a paging control that lets the user navigation to the previous page, next page, etc -->
@Html.PagedListPager((IPagedList)ViewBag.OnePageOfVacations, page => Url.Action("Vacations", new { page }))

我的Web服务中有一个方法"GetUserAbsence()",该方法仅返回我需要的那条记录数.现在我只有一页,无法显示其他结果.我的问题是-如何将此解决方案与PagedList连接起来,或者如何在此解决方案上创建新的分页?

I have a method in my webservice "GetUserAbsence()" which return to my only that number of records which I need. Now I have only one page and I cannot show other results. There is my question - how to connect this solution with PagedList or maybe how to create new paging on this solution?

推荐答案

MVC.PagedList具有StaticPagedList方法,该方法允许您提供IEnumerable<T>而不是IQuerable<T>,但是您必须知道记录的总数.在您的情况下,将是

MVC.PagedList has a StaticPagedList method that allow you to provide IEnumerable<T> rather than IQuerable<T>, but you must know the total number of records. In your case it would be

var vacations = client.GetUserAbsence(SessionManager.CurrentToken, quantity, pageNumber);
int totalItems = .... // see following note
ViewBag.OnePageOfVacations = new StaticPagedList<yourModel>(vacations, pageNumber, quantity, totalItems);

这意味着您的服务将需要包括另一个方法,该方法返回总记录数(但是应该很快),或者您可以修改GetUserAbsence()方法以返回一个对象,该对象包含已过滤集合的属性以及总记录的值.

It means that your service would need to include another method that returns the count of total records (but that should be very fast), or you could modify the GetUserAbsence() method to return an object containing properties for the filtered collection plus a value for the total records.

侧面说明:我建议您将强类型模型传递给视图,而不要使用ViewBag,以便控制器代码为

Side note: I recommend that you pass a strongly typed model to the view rather that using ViewBag, so that the controller code is

....
var model = new StaticPagedList<yourModel>(vacations, pageNumber, quantity, totalItems);
return View(model);

并在视图中

@model PagedList.IPagedList<yourModel>
....
@foreach (var v in Model)
{
    ....
}
....
@Html.PagedListPager(Model, page => Url.Action("Vacations", new { page }))

这篇关于分页.net MVC-无需从WebService下载所有记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆