在ASP中的部分视图缓存。净MVC 3

时间:2021-05-19 03:17:37

How can I cache the output of the PartialViews in ASp.NET MVC 3? I know I can decorate the action with [OutputCache] attribute but what I just want to include the @OutputCache right into the PartialView as shown below:

如何缓存ASp中部分视图的输出。净MVC 3 ?我知道我可以用[OutputCache]属性来修饰这个动作,但我只想把@OutputCache添加到PartialView中,如下所示:

@OutputCacheAttribute

@model MvcApplication1.Models.someViewmodel

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>



@Html.Partial("_MyPartialView")

1 个解决方案

#1


22  

This cannot be done. You need to use the Html.Action helper to render a child action decorated with the [OutputCache] attribute and which will render the partial.

不能做到这一点。您需要使用Html。用[OutputCache]属性修饰的子动作,并呈现局部动作。

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

and then include the partial:

然后包括部分:

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

#1


22  

This cannot be done. You need to use the Html.Action helper to render a child action decorated with the [OutputCache] attribute and which will render the partial.

不能做到这一点。您需要使用Html。用[OutputCache]属性修饰的子动作,并呈现局部动作。

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

and then include the partial:

然后包括部分:

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