I’m implementing a basic search feature for a site I’m working on. The cleanest way so far seems to be to create an action with a method signature similar to:
// // GET: /BeautySchoolDropouts/Search?page=2&q=grease public ActionResult Search(int? page, string q) { //Implementation }
I have some code then on the search results page that provides the paging links similar to:
<%= Html.RouteLink('<< Previous Page', new { page = (Model.PageIndex - 1) }) %> <%= Html.RouteLink('Next Page >>', new { page = (Model.PageIndex + 1) }) %>
Since I’m supplying the route values as part of the RouteLink method, is it impossible to keep the q=whatever part of the querystring? Right now, the links generate /BeautySchoolDropouts/Search?page=2 only, which obviously causes issues because I have no idea what the search was for.
I think this would work assuming you are putting the query string in the model somewhere (this code uses Model.QueryString; Note when I say ‘query string’ here, I’m talking about the query parameter, not the HTTP query string); I don’t think you want to persist the whole query string because then you’d get ?page=1&q=whatever&page=2&page=3.