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

joh*_*doe 11 asp.net-mvc

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

@OutputCacheAttribute

@model MvcApplication1.Models.someViewmodel

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>



@Html.Partial("_MyPartialView")
Run Code Online (Sandbox Code Playgroud)

Dar*_*rov 22

这是不可能做到的.您需要使用Html.Action帮助器来呈现使用该[OutputCache]属性修饰的子动作,并且该动作将呈现部分动作.

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

然后包括部分:

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

  • 三年后,这已经可以了吗? (2认同)