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!!
我在MVC应用程序中有禁用缓存的代码。我正在使用下面的响应头。除了Chrome之外,它们似乎适用于所有浏览器(目前使用的是31.0.1650.48版本)。用户可以提交带有模型值的表单。当他们点击后退按钮时,我需要页面重新载入一个空白模型。由于请求击中了操作并返回了空白模型,因此头文件部分工作。然而,视图并没有更新。上一个员额的价值正在保留。我已经尝试过清除ModelState,但是这行不通。有什么建议吗?提前谢谢! !
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()
2 个解决方案
#1
2
Turning off autocomplete for the form fixed this for me. I appreciate all the input!
关闭自动完成为表格修复了这个为我。我感谢所有的输入!
<form autocomplete="off"></form>
#2
0
There is a way to do this in javascript for everything in one go like so...
有一种方法可以在javascript中实现这一点,比如……
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.
只需在表单中添加id,所有输入将在页面加载时重置。不管它是第一次访问还是返回按钮,点击页面被缓存。与您的解决方案最大的不同在于“自动完成属性在HTML5中是新的”。这意味着它在旧的浏览器中是不受支持的,尽管它做了你想做的事情,它也阻止了用户自动填充字段。例如,对于文本类型的输入,用户将不会看到他们在之前或其他页面上输入的单词的提示。你可以在这里看到一个例子。
#1
2
Turning off autocomplete for the form fixed this for me. I appreciate all the input!
关闭自动完成为表格修复了这个为我。我感谢所有的输入!
<form autocomplete="off"></form>
#2
0
There is a way to do this in javascript for everything in one go like so...
有一种方法可以在javascript中实现这一点,比如……
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.
只需在表单中添加id,所有输入将在页面加载时重置。不管它是第一次访问还是返回按钮,点击页面被缓存。与您的解决方案最大的不同在于“自动完成属性在HTML5中是新的”。这意味着它在旧的浏览器中是不受支持的,尽管它做了你想做的事情,它也阻止了用户自动填充字段。例如,对于文本类型的输入,用户将不会看到他们在之前或其他页面上输入的单词的提示。你可以在这里看到一个例子。