jqueryrazorwebmatrix

Webmatrix - Persisting query strings across pages


I've just finished setting up pagination on one of my search pages, but i'm having issues because the query-strings (that i use to build my search query) are lost when i click between pages. Is there an easy method of persisting them, or will i need to write some fancy javascript to do this?

For example, i might have a page with the following URL localhost\search?qstring1=1&qstring2=2

At the bottom of the page, i have the following code, which provides links to where i need to persist these query strings.

for (var i = 1; i < totalPages + 1; i++){
    <li><a href="/search1/@i">@i</a></li>
}

Solution

  • You can get access to the current query string with Request.QueryString. Something like this ought to do the trick:

    for (var i = 1; i < totalPages + 1; i++) {
        <li><a href="/search/@i?@Request.QueryString">@i</a></li>
    }
    

    Your links should then end up looking like this:

    /search?x=1&y=2
    /search/2?x=1&y=2
    /search/15?x=1&y=2
    

    UrlData[0].AsInt(1) should have your page number which defaults to page one, and your query string should persist.