0

I'm using a pagination method for displaying search results returned from my database. I'm creating an Html.ActionLink for each extra page, and what I'm wondering is, how do you put the string that was searched into the ActionLink? Below is part of my partial view that I'm populating a div with. It comes after the results so the user can select another page of results.

            <% if (Model.HasNextPage)
               {  %>                
                 <% for (var i = 1; i < Model.TotalPages; i++)
                    { %>
                        <%= Html.ActionLink(i.ToString(), "MyPartialPage", "MyController", new { searchString = Cache[searchString], page = i }, new { @id = "SearchResults" })%>
                 <% } %>
            <% } %>

MyController looks like:

    public ActionResult SearchResults(string searchString, int? page)
    {
        var theResults = driverRepository.GetResults();

        var searchResults = drivers.Where(q => q.Filename.Contains(searchString));

        var paginatedDrivers = new PaginatedList<Driver>(searchResults, page ?? 0, pageSize);

        return View("SearchResults", paginatedDrivers);   
    }

There is a textbox and button. The user types the search and presses the button which fires some javascript that gets the text from the textbox and posts to SearchResults accordingly.

I want to keep what they searched so when I build my ActionLinks, the search parameter is still present.

Darcy
  • 5,228
  • 12
  • 53
  • 79
  • 1
    Check out this question/answer: http://stackoverflow.com/questions/1663616/paging-search-results-with-asp-net-mvc/1664269#1664269 – Omar Feb 26 '10 at 14:28
  • Caching is something I would like to do, but I do have a problem. In order for caching to work I would need to create a unique key for each searchString, which is fine until I get to the HasNextPage part of my view, which creates the actionlink. Since the actionlink needs the searchString parameter to pass, I was going to use the key that was searched for for the links. HOW do I get this key though? – Darcy Feb 26 '10 at 16:57

2 Answers2

0

You could put the pager links inside the same form that contains the search box and replace them with submit or image buttons.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
0

What I ended up doing was, adding a parameter to my PaginatedList class that contains the search string, then setting my searchString in the actionresult to the model.seachString, like so:

        <% if (Model.HasNextPage)
           {  %>                
             <% for (var i = 1; i < Model.TotalPages; i++)
                { %>
                    <%= Html.ActionLink(i.ToString(), "MyPartialPage", "MyController", new { searchString = this.Model.SearchString, page = i }, new { @id = "SearchResults" })%>
             <% } %>
        <% } %>

and my controller looks like

public ActionResult SearchResults(string searchString, int? page)
{
    var theResults = driverRepository.GetResults();

    var searchResults = drivers.Where(q => q.Filename.Contains(searchString));

    var paginatedDrivers = new PaginatedList<Driver>(searchString, searchResults, page ?? 0, pageSize);

    return View("SearchResults", paginatedDrivers);   
}
Darcy
  • 5,228
  • 12
  • 53
  • 79