我有一个对动作方法进行 ajax 调用的视图。
$.ajax({
url: url to action,
type: 'GET',
cache: false,
dataType: 'html',
success: function(result) {
$("#divPatient").html(result);
$("#divPatient").show("blind", { }, 2000);
$("#loadingImage").hide();
PrepPatientHtml();
}
});
如您所见,action 方法返回 html。该站点由 SQL 数据库驱动,该数据库在更改时会影响操作的输出。我添加了一个 NoCache 动作过滤器
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);
}
}
出于某种原因,当对支持视图的数据库进行更改时,缓存永远不会失效。有人有什么想法吗?视图相当简单:
@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>
}