Alex
Alex

Reputation: 11

Handling "Back Button"-Navigation in a ASP.NET MVC website

We're are implementing a simple phone book website with ASP.NET MVC. It will be optimized for mobile devices using the JQuery Mobile Framework. The website basically consists of a search form, a result page and a details page.

The Workflow

Fill the search form, post to result controller, show results. User selects a result and get details for this single result. The details are displayed on a separate site. The site gets the id of the single result as get parameter, fills a DetailModel and pass the model to the view to present it.

Works fine

Doing a search, getting results, showing the details works fine. Doing a search, getting results, navigate back to the search, refine fields, search.. works find.

The Problem

Doing a search, getting results, showing the details and then go back to the result page ... how do i get the latest search results? It's a GET request. The model (RequestResults) is empty. How should I store this information? Sessions? Which is the "right way" to implement such a requirement?

Thanks for any hint in the right direction alex

Update 18:12

To clarify how the current workflow is: The search form is submitted via POST to the result page. The result pages get an instance of a SearchRequestModel and runs the search against the database. Then the results are displayed. Each result contains a link to the details page showing more information on this particular result. The link is called via GET. After following this link every information about my inital search request and the result are lost. How do I get back to the result view?

Does anybody know an implementation facing this problem?

Upvotes: 0

Views: 7191

Answers (3)

Francisco Goldenstein
Francisco Goldenstein

Reputation: 13767

The problem is that the page is cached by the browser and because of that the controller action is not executed. In ASP.NET MVC, you can avoid this situation decorating an action with the following code:

[OutputCache(NoStore = true, Duration = 1)]

This tells the browser not to use the cache and executes the server code. You can also specify this for all actions but it's not recommendable because in many cases it's better to take the page from the cache because it cause no problems and minimizes the use of resources.

Upvotes: 5

Mark
Mark

Reputation: 291

Try changing your form to a GET rather than a POST. This will put all your form parameters into the querystring. This may or may not be ideal in your case, but would solve the problem as the browser remembers you history.

This is how Google does.

Hope this helps,

Mark

Upvotes: 0

Flater
Flater

Reputation: 13783

The problem you're facing is that most browsers cache the previous page, and when you go back, no actual network connection is being used to load the page, the browser doesn't even consider checking back on the server.

Presumably, you can't control client-side browser settings. The only easy solution (that's relative) I've found is that you load that page via Ajax. Keep in mind, IE tends to cache Ajax calls as well if you're calling the same URL. Therefore, you can do an ajaxcall and add a useless parameter at the end that keeps changing (a timestamp for example).

This is an (MVC 2) example

$(document).ready(function (event) {
        $.ajax({
            url: '<%= Url.Action("LoadPageContents", "Controllername") %>',
            data: "ID=" + *thevalueyouneed* + "&date=" + new Date().getTime(),
            success: function (data) {
                //In my case, a partial View is returned
                $("#containingDiv").html(data);
            }
        });
    });

Of course, if this page is supposed to contain a huge amount of data, it might not be suitable to transfer all that via Ajax.

There is the far simpler solution of adding a no-cache meta tag to the page, like so:

<META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE">

But I've never gotten that to work properly cross-browser.

Upvotes: 0

Related Questions