【问题标题】:Partial Views Caching in ASP.NET MVC 3ASP.NET MVC 3 中的部分视图缓存
【发布时间】:2011-01-20 17:35:09
【问题描述】:

如何在 ASp.NET MVC 3 中缓存 PartialViews 的输出?我知道我可以使用 [OutputCache] 属性来装饰动作,但我只想将 @OutputCache 包含在 PartialView 中,如下所示:

@OutputCacheAttribute

@model MvcApplication1.Models.someViewmodel

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>



@Html.Partial("_MyPartialView")

【问题讨论】:

    标签: asp.net-mvc


    【解决方案1】:

    这是无法做到的。您需要使用 Html.Action 帮助器来呈现一个用 [OutputCache] 属性装饰的子动作,它将呈现部分。

    public class MyController : Controller
    {
        [OutputCache(Duration = 3600)]
        public ActionResult Index()
        {
            return View();
        }
    }
    

    然后包括部分:

    @model MvcApplication1.Models.someViewmodel
    @{
        ViewBag.Title = "Index";
    }
    <h2>Index</h2>
    @Html.Action("Index", "My")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-03
      • 2012-05-15
      • 1970-01-01
      相关资源
      最近更新 更多