0

I have a view that makes an ajax call to a action method.

$.ajax({
url: url to action, 
type: 'GET',
cache: false,
dataType: 'html',
            success: function(result) {
                $("#divPatient").html(result);
                $("#divPatient").show("blind", { }, 2000);
                $("#loadingImage").hide();
                PrepPatientHtml();
            }
        });

The action method returns html as you can see. The site is driven off a SQL database, which when changed should effect the output of the action. I've added a NoCache Action Filter

public class NoCache : ActionFilterAttribute
    {
        public override void OnResultExecuting(ResultExecutingContext filterContext)
        {
            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();

            base.OnResultExecuting(filterContext);
        }
    }

For some reason, the cache never gets invalidated when a change is made to the database supporting the view. Anyone have any thoughts? The view is fairly simple:

@using (Html.BeginForm("Save", "Home", FormMethod.Post, new { id = "frm" }))
{
    <div style="padding: 10px;">
        <h1 id="questionsHeader">@Model.FullName (@Model.Dob)</h1>
        @for (var i = 0; i < Model.Episodes.Count; i++)
        {
            @Html.EditorFor(model => model.Episodes[i])
        }
    </div>
    <div style="padding-top: 10px; border-top: solid 1px #666666; margin-top: 5px; text-align: right;">
        <input type="submit" id="btnSaveAnswers" value="Save Answers" />        
    </div>
}
jsteve81
  • 735
  • 2
  • 6
  • 16

2 Answers2

0

Use this annotation for your action:

[OutputCache(Duration = 0)]
Robby Shaw
  • 4,725
  • 1
  • 14
  • 11
0

I don't believe it would be the framework or as you called "entity framework" (probably you wanted to say something else).

Did you to debug your application to see if the action was actually being executed?

If yes, do you have objects cached that end up producing the same result?

My guess is that is your browser that is caching the result.

Did you try this:

response.setHeader( "Pragma", "no-cache" );
response.setHeader( "Cache-Control", "no-cache" );
response.setDateHeader( "Expires", 0 );

If it doesn't work, try changing the URL by adding a random parameter like:

+ '&uid=' + uniqueId()

where uniqueId() is a function that gets you a unique string in time like the number of ticks of the time urlencoded.

Although this last option may not be best, it will certainly be the easiest, fastest and will work for sure.

Fabio Milheiro
  • 8,100
  • 17
  • 57
  • 96