views:

44

answers:

2

Hi all,

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.

A: 

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
A: 

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