2

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?

3
  • You'll need to use ajax or something similar otherwise the view can't get updated, it's totally static and can't call back to the server and tell it to send more data. I don't have much time to send you more info but this may help stackoverflow.com/questions/8517071/… Commented Jul 14, 2016 at 11:56
  • 1
    If you know the total number of records, you can use StaticPagedList, for example, ViewBag.OnePageOfVacations = new StaticPagedList<yourModel>(vacations, pageNumber, quantity, totalItems); (may mean you need a 2nd service method that returns .Count() but that should be very fast (alternatively you could modify the method to return an object containing the records and the count) Commented Jul 14, 2016 at 12:09
  • Please make from comment an answer. Thanks Stephen! It Works! Commented Jul 14, 2016 at 12:22

1 Answer 1

3

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 }))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.