ASP.NET MVC 3中的部分视图缓存

11

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

@OutputCacheAttribute

@model MvcApplication1.Models.someViewmodel

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>



@Html.Partial("_MyPartialView")
1个回答

22

无法直接实现此功能,您需要使用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")

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接