Reputation: 6813
I'm using MVC3 - i have a javascript function that uses jQuery get() to get a PartialView from a controller.
The problem is that it's being cached and i keep getting stale content back.
I've tried [OutputCache(Duration=0)] on the action, thinking it would prevent it caching, but no joy. Could it be the client caching it too?
EDIT:
I've recently been using another way to prevent caching which may be useful to some.
$.get("/someurl?_="+$.now(),function(data) {
// process data
});
It's obviously not as clean, but because each request passes a _=12345678
(timestamp) it's never cached.
Hope it helps.
Upvotes: 20
Views: 14657
Reputation: 1231
It seems by default that all MVC 3 partial views are automatically cached, but you can control this from the controllers for each partial view that is returned with an attribute (or annotations as they are called in Java) in front of the action:
[OutputCache(Duration = 0)]
public ActionResult PersonEdit(string id)
{
// do query and fill editvm here
return PartialView("PersonEdit",editvm);
}
So the duration is set to zero. There are probably many other attributes that can be set to turn off caching, but so far this seems to work for me on an individual basis.
Upvotes: 9
Reputation: 6813
thanks to both of you, the first one still cached with type="GET" even with cache:'false' specified. That's using chrome and local IIS7.
I ended up with
$.ajax({
url: '@Url.Action("GetMyPartialView","MyController")/' + parameterId,
type: 'POST',
cache: 'false',
success: function (result) {
$('#dynamicContentDiv').html(result);
}
});
Works fine, thanks for you responses.
Upvotes: 5
Reputation: 1038720
GET requests could be automatically cached by the browser so you could use the .ajax()
function which contrary to the .get()
function allows you to disabled caching:
$.ajax({
url: '/foo',
type: 'GET',
cache: 'false',
success: function(result) {
}
});
Another possibility is to use POST:
$.post('/foo', function(result) {
});
Upvotes: 21
Reputation: 6005
IE is particularly bad about that. You can disable all AJAX caching with the following:
$.ajaxSetup({
cache: false
});
Upvotes: 19