Paging .net MVC - without download all records from WebService

后端 未结 1 1026
野的像风
野的像风 2021-01-21 16:28

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:

publi         


        
相关标签:
1条回答
  • 2021-01-21 17:13

    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 }))
    
    0 讨论(0)
提交回复
热议问题