2017-08-08 66 views
0

在我的MVC項目中,除了顯示當前時間(這是動態的)之外,還有一些靜態內容的頁面。我想要緩存這個頁面,但是由於時間原因我無法緩存。動態更新MVC中緩存頁面的一部分

我嘗試使用DonutCatching,但它不工作。 以下是我嘗試使用DonutCatching的代碼。

主控制器:-Search.cs

[OutputCache(Duration=3600)] 
public ActionResult Index() 
{ 
    return View("Main.cshtml"); 
} 
public ActionResult Part() 
{ 
    return View("Date.cshtml",DateTime.Now); 
} 

Main.cshtml

@using DevTrends.MvcDonutCaching; 
<div></div> 
<div></div> 
@Html.Action("Part","Search",true) 
<div></div> 
<div></div> 

Date.cshtml

@model DateTime 
<h2>@Model</h2> 

我想緩存日Main.cshtml但部分更新每次請求Main.cshtml。任何解決方案或我在上面做錯了什麼?

+0

你爲什麼不乾脆使用JavaScript是什麼? – Robert

+0

Javascript工作正常,但如果我想顯示廣告而不是日期,那我該怎麼辦? – yajiv

回答

1

我不認爲這是可能的,因爲整個頁面緩存服務器端。您可以嘗試基於計時器的Ajax負載(setTimeout類型js),或者放置$ .ready()處理程序而不是部分視圖。然後在緩存的主頁完成加載後加載該部分。 檢查jQuery的這個函數: http://api.jquery.com/load/

<div id="dynamicsection"> 
<script> 
$("#dynamicsection").load("@Url.Action("Part", "Search")"); 
</script> 

讓我知道這是否有助於