Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Partial Views Caching in ASP.NET MVC 3

Tags:

asp.net-mvc

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:

@OutputCacheAttribute

@model MvcApplication1.Models.someViewmodel

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>



@Html.Partial("_MyPartialView")
like image 509
johndoe Avatar asked Jan 20 '11 17:01

johndoe


1 Answers

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.

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")
like image 183
Darin Dimitrov Avatar answered Oct 06 '22 01:10

Darin Dimitrov