VinnyG
VinnyG

Reputation: 6911

Generic "back" button in a MVC3 application

Is there a good way to create a generic back button in my asp.net MVC3 application?

For the moment, I have a different action on each of my button and when there's more possibilities, I add redirectTo in the Url so I can redirect back to this Url. It not the best solution since I have to add every possibility and I will probably forget to add it to a specific form.

I also taught of using History.Back in js but sometimes it will redirect to a form that will redirect back to an other (as an exemple the list of inscriptions redirect to add an inscription if there is no inscription, then clicking on the back buttons should redirect to the list of inscriptions if there's is at least one inscription but to the list of courses if there's no inscription). The back button will also close a popup sometime.

Maybe it could be something in a BaseController that would execut on the OnActionExecuting() or something in the Global.asax like a GlobalFilter?

Any idea of how I could do this?

Upvotes: 1

Views: 4577

Answers (3)

Avtandil Kavrelishvili
Avtandil Kavrelishvili

Reputation: 1757

<a class="back_button" href="@Request.UrlReferrer"><i class="icon-circle-arrow-left"></i>@ViewResources.ShipingLine.back</a>

You can use href="@Request.UrlReferrer" to back page first step.

I have already tested it and it working well.

Avtandil kavrelishvili

Upvotes: 2

VinnyG
VinnyG

Reputation: 6911

What I did finaly is to overwrite the button in javascript :

    $('.cancel-btn').click(function(ev) {
        ev.preventDefault();
        history.back();
    });

It works fine, even for the double redirection, it get back to the first page and not to the one that redirect to the last one. Since it's an application and you have to loggin first, you will almost always have a page to go back to.

Upvotes: 1

Oliver
Oliver

Reputation: 9002

You could use Request.UrlReferrer to easily access where a user has come from.

You could have something along the lines of:

<button type="button" onclick="window.location='@Request.UrlReferrer.AbsoluteUri'" >Back</button>

However the UrlReferrer will not be available if the user has directly navigated to the page.

Upvotes: 3

Related Questions