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

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

问题描述

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

控制器代码:

public ActionResult Vacations(int? page){int pageNumber = 页 ??1;整数数量 = 5;使用 (MyWebService.MyWebServiceClient client = new MyWebService.MyWebServiceClient()){var 假期 = client.GetUserAbsence(SessionManager.CurrentToken,quantity, pageNumber);var onePageOfVacations = Vacations.ToPagedList(pageNumber,quantity);ViewBag.OnePageOfVacations = onePageOfVacations;}返回视图();}

查看代码:

假期

<div><table class="table tablesorter table-striped"><头><tr><th>Data od</th><th>数据做</th><th>Ilość dni</th></tr></thead>@{foreach(ViewBag.OnePageOfVacations 中的 var v){<tr><td>@v.From_Date</td><td>@v.To_Date</td><td>@v.Element_Name</td></tr>}}</tbody><脚><tr><th>Data od</th><th>数据做</th><th>Ilość dni</th></tr></tfoot>

<!-- 输出一个分页控件,让用户导航到上一页、下一页等 -->@Html.PagedListPager((IPagedList)ViewBag.OnePageOfVacations, page => Url.Action("Vacations", new { page }))

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

解决方案

MVC.PagedList 有一个 StaticPagedList 方法,允许您提供 IEnumerable 而不是 IQuerable,但你必须知道记录的总数.在你的情况下是

var leaves = client.GetUserAbsence(SessionManager.CurrentToken,quantity, pageNumber);int totalItems = ....//见下面的注释ViewBag.OnePageOfVacations = new StaticPagedList(vacations, pageNumber,quantity, totalItems);

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

旁注:我建议您将强类型模型传递给视图,而不是使用 ViewBag,以便控制器代码为

<代码>....var model = new StaticPagedList(vacations, pageNumber,quantity, totalItems);返回视图(模型);

在视图中

@model PagedList.IPagedList....@foreach(模型中的 var v){....}....@Html.PagedListPager(Model, page => Url.Action("Vacations", new { page }))

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.

Controller code:

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

View Code:

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

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

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.

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

and in the view

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

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

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