Reload this Page Back - MVC 4

What would be an easy way to make my ActionResult always fire when I delete this page? For example, when you click back on the browser, this will not work.

 public ActionResult Index() { //do something always return View(); } 
+7
asp.net-mvc-4
source share
3 answers

Disabling the cache in ActionResult causes the page to refresh every time, rather than showing the cached version.

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

Now, when you click the back button of browsers, it strikes every time.

+19
source share

You can try the onunload and ajax event:

 <script> window.onunload = function () { $.ajax({ url: '/ControllerName/Index', type: 'POST', datatype: "json", contentType: "application/json; charset=utf-8" }); }; </script> 
+1
source share

Adding this code to my HTML is great for me:

 <input id="alwaysFetch" type="hidden" /> <script> setTimeout(function () { var el = document.getElementById('alwaysFetch'); el.value = el.value ? location.reload() : true; }, 0); </script> 

This may come in handy for those who prefer not to deal with server-side code rather than the decision made.

All he does is assign a value to the hidden input on the first load, which will remain on the second load, in which case the page will be refreshed.

0
source share

All Articles