I have a grid with contact information which I need to be able to page through.
All the plumbing is already in place, with one last detail. Paging is done through a simple p Querystring parameter, e.g. www.myurl.com/grid?p=3 would be the third page; the repository is automatically fetching the right data, and also the total count of items. The size of each page is defined somewhere else and I don't need to worry about that in the query string.
However, I support searching etc. as well. The search term searched for in denoted as q in my Querystring. So now I can have a combination: www.myurl.com/grid?q=tom&p=2 which searches for "tom" and pulls the second page of results.
The problem I face now, since the q (or other) parameters may be present in the query string, how do I create a pager for this (which would need to keep the original parameters, so if I click on "page 2" it needs to go from
www.myurl.com/grid?a=1&b=xyz&q=tom
to
www.myurl.com/grid?a=1&b=xyz&q=tom&p=2
How can I do this?
I asked a similar question yesterday. Maybe you want to check out Preserve data in .net mvc
following is the code copied from Steve Sanderson's book
public static class PagingHelpers
{
public static string PageLinks(this HtmlHelper html, int currentPage,
int totalPages, Func<int, string> pageUrl)
{
StringBuilder result = new StringBuilder();
for (int i = 1; i <= totalPages; i++)
{
TagBuilder tag = new TagBuilder("a"); // Construct an <a> tag
tag.MergeAttribute("href", pageUrl(i));
tag.InnerHtml = i.ToString();
if (i == currentPage)
tag.AddCssClass("selected");
result.AppendLine(tag.ToString());
}
return result.ToString();
}
}