Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Refresh Page on Back Click - MVC 4

What would be a simple way to force my ActionResult to always fire when I hit this page? On browser back click, for instance, this will not execute.

public ActionResult Index()
{
    //do something always

    return View();
}
like image 734
aw04 Avatar asked Mar 18 '14 13:03

aw04


2 Answers

Disabling cache on the ActionResult forces the page to refresh each time rather than rendering the cached version.

[OutputCacheAttribute(VaryByParam = "*", Duration = 0, NoStore = true)]
public ActionResult Index()
{
    //do something always

    return View();
}

Now when you click the browsers back button, this is hit every time.

like image 146
aw04 Avatar answered Oct 05 '22 00:10

aw04


You could try the onunload event and ajax:

<script>
   window.onunload = function () {
      $.ajax({
         url: '/ControllerName/Index',
         type: 'POST',
         datatype: "json",
         contentType: "application/json; charset=utf-8"
      });
   };
</script>
like image 41
Dumisani Avatar answered Oct 05 '22 01:10

Dumisani