避免从缓存中加载View

时间:2014-09-17 16:50:48

标签: c# asp.net-mvc asp.net-mvc-4

如何避免使用缓存加载View?

我尝试将[OutputCache(Duration = 1)]放在我的方法之前,该方法通过ActionResult返回视图,但没有成功。

    [OutputCache(Duration = 1)]
    public ActionResult Receita(string id, string periodo)
    {
        // Do my stuff

        var receita = new ReceitaAssunto()
        {
            // More stuff
        };

        return View(receita);
    }

当我通过方法参数传递一个新值时,应该在我的视图中显示这个值,但它还没有刷新,总是显示旧值。

查看

@model Dashboard.Domain.ClasseTipada.ReceitaAssunto

<ul class="list-group clear-list m-t">

@{
     var i = 1;
     foreach(var elemento in Model.ReceitaPorTipoReceita)
     {
        <li class="list-group-item fist-item">
            <span class="pull-right">
                <span id="txtTextoValorLocacao">@elemento.Valor.ToString("C", new System.Globalization.CultureInfo("pt-BR"))</span>
                </span>
                <span class="label label-success">@i</span>@elemento.DescricaoOrigem
        </li>
        i++;
      }
      i = 0;
 }
 </ul>

更新

我看到了使用Firebug的请求,结果它正是我想要的,但它不会在我的视图中呈现。

enter image description here

我如何看待(看一下这些值),这些值仅在第一页加载时才为真

enter image description here

JS

    $("#btnTrimestral").on("click", function () {           
        GeradorGrafico.URLJson = "@Url.Action("Receita", new { periodo = "trimestral" })";
        GeradorGrafico.init();
    });

1 个答案:

答案 0 :(得分:0)

将其放入Application_BeginRequest().

中的Global.asax.cs
    HttpContext.Current.Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1));
    HttpContext.Current.Response.Cache.SetValidUntilExpires(false);
    HttpContext.Current.Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
    HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
    HttpContext.Current.Response.Cache.SetNoStore();