Click here to Skip to main content
15,913,685 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to create a query string, which allows all the results to be filtered using pagination. (i.e. api/test?name=frs&page=2)

for example:
if the query returns 20 records of data, i would like the each page to show 10 lines of records, hence if I call [api/test?name=frs&page=1], and it would show me first 10 records of the query.
if I query [api/test?name=frs&page=2], then the page would now show me second set of 10 records.

Currently, I have tried writing some lines of code for the page filter but I am still unable to get all the records from the querystring (i.e. api/test?name=frs&page=1). This query returns total number of 20 records and it only shows first 10 and when I add in the page filter --> api/test?name=frs&page=2, it still shows the first set of 10 records, instead of showing the second set of 10 records.

C#
public HttpResponseMessage get([FromUri] Query query )
     {
            if (User.IsInRole("admin"))
         {
         int pageSize = 10;
         int page = 0;

     //code for getting data (i.e. name, tag etc)

             var totalCount = Data.Count();
             var totalPages = (int)Math.Ceiling((double)totalCount / pageSize);

             //var urlHelper = new UrlHelper(Request);
            // var prevLink = page > 0 ? urlHelper.Link("DefaultApi", new { page = page - 1 }) : "";
            // var nextLink = page < totalPages - 1 ? urlHelper.Link("DefaultApi", new { page = page + 1 }) : "";

             var pageValue = Request.RequestUri.ParseQueryString().Get("page");
             int currentPage;
             if (!int.TryParse(pageValue, out currentPage))
             {
                 currentPage = 0;
             }

             Data = Data.OrderByDescending(c => c.UploadDate);

             var data = Data.Skip(pageSize * page).Take(pageSize).ToList();

             if (!data.Any())
             {
                 var message = string.Format("No data found");
                 return Request.CreateErrorResponse(HttpStatusCode.NotFound, message);
             }

             //return Request.CreateResponse(HttpStatusCode.OK, data);
            // return Request.CreateResponse(HttpStatusCode.OK, new { totalCount, totalPages, prevLink, nextLink, data });
             return Request.CreateResponse(HttpStatusCode.OK, new { totalCount, totalPages, pageValue, data });
         }
            return Request.CreateErrorResponse(HttpStatusCode.NotFound, "Access Denied");
     }


Please help.

I hope i have clarified the pagination issue clearly, if not, please do let me know. Thank you for your reply
Posted

1 solution

It looks like you using the wrong variable in your skip method. The page variable is always set to 0 and the one you get from your query string is named currentPage after prasing. Think you forgot you created the first variable.

var data = Data.Skip(pageSize * page).Take(pageSize).ToList();

Fix:
var data = Data.Skip(pageSize * currentPage).Take(pageSize).ToList();


You could also change the output parameter to page in your TryPrase method. Either way works.
 
Share this answer
 
v2
Comments
miss786 17-Jul-14 4:38am    
Thank you so much for correcting this. I really appreciate your help. Many thanks for solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900