繁体   English   中英

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

[英]Partial Views Caching in ASP.NET MVC 3

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

@OutputCacheAttribute

@model MvcApplication1.Models.someViewmodel

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>



@Html.Partial("_MyPartialView")

这是不可能做到的。 您需要使用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")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM