2012-01-05 73 views
1

我使用的是帶有剃鬚刀的MVC 3。我有一些從主視角來看的部分視圖。在部分視圖中,我想顯示一些DB值。當我更改DB值時,它會顯示緩存中的舊值。那麼,我如何停止部分視圖緩存?MVC 3防止部分查看緩存

@Html.Partial("_myPartialView", Model) 

THX

回答

0

上面的代碼不使用緩存爲默認值。如果您使用

@Html.Partial("_myPartialView", Model) 

它使得利用模型對象數據,而無需任何緩存_myPartialView。你的問題必須由其他事情引起。也許構建Model對象的數據檢索代碼使用一些數據層緩存?發佈更多的代碼會有幫助。

1
$(function() { 
$.ajaxSetup ({ 
     // Disable caching of AJAX responses 
     cache: false }); 
)}; 

應該這樣做!

0

根據設置的不同,也許一種可能的方法是在部分視圖中禁用緩存,將其作爲單獨的客戶端調用分離出來,即分解爲jQuery/Ajax。

否則如何在主題上的這種變化。

花了一點時間後,回到MVC中找出這一個。只需將緩存設置直接放入部分標題視圖即可。就像在顯示用戶名時一樣。不需要全局或服務器端代碼。

唯一的問題是一旦頁面被緩存,登錄後不會立即刷新。但是我們在初次瀏覽產品時需要保持速度。在我們的情況下折衷。

@if (Request.IsAuthenticated) 
{ 
     @* when we are authenticated, don't cache any more! *@ 
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache); 
HttpContext.Current.Response.Cache.SetNoStore(); 
HttpContext.Current.Response.Cache.SetNoServerCaching(); 
     @*@Html.Raw(DateTime.Now.ToString())*@ 
@Html.ActionLink("Welcome " + (String.IsNullOrEmpty(((System.Security.Claims.ClaimsIdentity)User.Identity).FindFirstValue("UserName")) ? User.Identity.Name : ((System.Security.Claims.ClaimsIdentity)User.Identity).FindFirstValue("UserName")), "Index", "Manage", routeValues: new { Area = "Store" }, htmlAttributes: new { title = "Manage"}) 
} 
else 
{ 
}