Back button to previous partialview

2.4k views Asked by At

In my .Net MVC4 project the view contains partialviews, the partialviews replaced on another after each corresponding Ajax.Beginform with option InsertionMode = InsertionMode.Replace, So the senario is

  1. Partialview1 renders on first Ajax.Beginform call
  2. Submit form, partialview2 render on 2nd Ajax.beginform, so partialview1 is replaced by partialview2

3. A previous button recalls partialview1

Im wondering how to implement step3 that goes to previous partialview with something simple as history back.

1

There are 1 answers

4
teo van kot On

First Way: I think the best way to do that with browser Back button is to use history.pushState

How to implement it depends on your situation on View.

Some links about pushState:

http://rosspenman.com/pushstate-jquery/

https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Manipulating_the_browser_history

Second Way: If you don't want to use browsers Back buttom you can use js:

function getBack()
{
   $.ajax({
     type: "GET",
     url: '@Url.Action("ReturnPartialViewControllerMethod")',
     success: function(data) {
           // data is your view
          $('#divForParticialView').html(data);
     }
   });
}

and in your Html:

<a href="javascript:void(0);" onclick="getBack()">Get Back</a>