20

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.

sambomartin
  • 6,663
  • 7
  • 40
  • 64

4 Answers4

21

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) {

});
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
19

IE is particularly bad about that. You can disable all AJAX caching with the following:

$.ajaxSetup({
    cache: false
});
Josh Anderson
  • 5,975
  • 2
  • 35
  • 48
9

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.

Johncl
  • 1,211
  • 1
  • 12
  • 13
  • 2
    [OutputCache(Duration = 0)] throws an error: "Duration must be a positive number" [OutputCache(NoStore=true)] works instead. – Neil N May 07 '12 at 15:32
  • Duration = 0 worked fine when I was doing MVC3, perhaps a later update has added this error? Still nice if NoStore=true also works then by all means use that! :) – Johncl May 10 '12 at 08:06
  • Could be, but I was also getting the error on a child action, and I have learned child actions are handled a little differently, which is annoying. – Neil N May 10 '12 at 14:18
5

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.

sambomartin
  • 6,663
  • 7
  • 40
  • 64
  • This is the only thing that worked for me with IE. Using cache: false didn't work, using the OutputStore attribute didn't work, other answers on other questions didn't work. Adding "?randomIdAddedToStopCaching=@Guid.NewGuid().ToString()" to the end of the URL was the only thing that worked. – Matt Aug 18 '17 at 02:12