qquinn80
qquinn80

Reputation: 41

ASP.NET MVC 4.0 Chrome Caching

I have code in place to disable caching in my MVC application. I'm using the following response headers. They seem to work in all browsers except for Chrome (currently using version 31.0.1650.48). Users are able to submit a form with model values. When they hit the back button I need the page to reload with a blank model. The headers appear to partially work since the request is hitting the action and returning the blank model. However the view isn't updating. The values from the previous post are being retained. I've tried clearing the ModelState but that doesn't work. Any suggestions? Thanks in advance!!

    filterContext.HttpContext.Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1))
    filterContext.HttpContext.Response.Cache.SetValidUntilExpires(False)
    filterContext.HttpContext.Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches)
    filterContext.HttpContext.Response.Cache.SetCacheability(HttpCacheability.NoCache)
    filterContext.HttpContext.Response.Cache.SetNoStore()

Upvotes: 2

Views: 1332

Answers (2)

fassetar
fassetar

Reputation: 620

There is a way to do this in javascript for everything in one go like so...

document.getElementById("YourFormID").reset();

Just add an id to your form and all your inputs will reset on page load. Regardless if its a first time visit or a back button click with the page being cached. The biggest difference from your solution and this is that "the autocomplete attribute is new in HTML5". Which means it is not supported in older browsers, and though it does what you want it also prevents the user from autofilling fields. Meaning that for example on text types inputs users will not see a suggestion of words they may have entered on previous or other pages. You can see an example here.

Upvotes: 0

qquinn80
qquinn80

Reputation: 41

Turning off autocomplete for the form fixed this for me. I appreciate all the input!

  <form autocomplete="off"></form>

Upvotes: 2

Related Questions