0

I've built a Search Page with 5 properties to filter on. When a user clicks on one of the results the detail page is loaded. Now I want to provide a "Back" button so the user can go back to the Search Page with the original filter.

I was thinking about using TempData to store the filter model. Tempdata is stored in session for only one call so the session won't be bloated after a while.

Is there a better sollution or do you guys have some suggestions? Let me know!

Edit: The search page will make use of ajax calls to page, sort or filter the data. So all this data will need to be stored if I want to navigate back from the detail page. Is TempData the best way?

Beejee
  • 1,836
  • 2
  • 17
  • 31

2 Answers2

2

Why not to use query-string for this? E.g. search request is submitted using <form /> element with method attribute set to "get". In this case you can easily restore the form state by just reading from the query-string, the code will be much simpler. Visitors also can easily bookmark the page and return to search results later.

View:

@model SearchResultSet;

<form method="get" action="/search">
    <input type="text" name="q" value="@Request.QueryString["q"]" />
    <input type="submit" value="Search" />
</form>

@if (Model.Total > 0)
{
    <ul>
        @foreach (var result in Model.Results)
        {
            <li>...</li>
        }
    </ul>
}

Model & controller:

public class SearchResultSet
{
    public IList<SearchResult> Results { get; set; }
    public long Total { get; set; }
} 

public class SearchController : Controller
{  
    public ActionResult Index(string q = "")
    {
        return View(GetModel(q));
    }

    private SearchResultSet GetModel(string searchQuery)
    {
        // Get search results
    }
}

Hope this helps.

volpav
  • 5,090
  • 19
  • 27
  • I am planning to use ajax calls for the paging, sorting and filtering on the Search page. The website will be used as an internal web app so bookmarking won't be needed. – Beejee Sep 19 '13 at 06:51
  • @Beejee You can use [History API](https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Manipulating_the_browser_history) and still leverage the query-string (because it's very transparent what's going on and not error-prone comparing to one-time session-based storage). As an alternative, you can try [jQuery BBQ](http://benalman.com/projects/jquery-bbq-plugin/) in conjunction with managing state via hashbang. The above solution also makes it much easier to write unit tests for your application. – volpav Sep 19 '13 at 06:59
  • The user will be able to sort on multiple properties. So i will have X filter inputs. How are you going to put all that in a query string? Normally i always bind to a viewmodel when using more than 2 properties. – Beejee Sep 20 '13 at 07:43
  • I didn't know that a querystring could be bound to a model like explained [here](http://stackoverflow.com/questions/17329342/does-model-binding-work-via-query-string-in-asp-net-mvc). This could come in handy yeah. How do you plan on providing the url to the next controller/action to use it for the 'Back' button? Since I will use ajax calls on the search page. History API won't contain the filter url/querystring. – Beejee Sep 20 '13 at 07:59
  • @Beejee You could make your model serializable to query-string like it's demonstrated [here](http://stackoverflow.com/questions/6848296/how-do-i-serialize-an-object-into-query-string-format). Overriding `ToString` method for this could be quite convenient. – volpav Sep 20 '13 at 20:30
2

Well, TempData retains value for one call, but you can retain the TempData value using TempData.Keep() until your Session expires.

TempData["YourKey"] = "SomeValue";
TempData.Keep("YourKey");

Hope it helps.

AthibaN
  • 2,087
  • 1
  • 15
  • 22