I am using Razor view engine in ASP.Net MVC 3 RC 2. This is part of my view city.cshtml
(drastically simplified code for the sake of simplicity in example)
<!-- in city.cshtml -->
<div class="list">
@foreach(var product in SQL.GetProducts(Model.City) )
{
<div class="product">
<div>@product.Name</div>
<div class="category">
@foreach(var category in SQL.GetCategories(product.ID) )
{
<a href="@category.Url">@category.Name</a> »
}
</div>
</div>
}
</div>
I want to cache this part of my output using OutputCache attribute, so I created an action ProductList with OutputCache attribute enabled
<!-- in city.cshtml -->
<div class="list">
@Html.Action("ProductList", new { City = Model.City })
</div>
and I created the view in ProductList.cshtml as below
<!-- in ProductList.cshtml -->
@foreach(var product in Model.Products )
{
<div class="product">
<div>@product.Name</div>
<div class="category">
@foreach(var category in SQL.GetCategories(product.ID) )
{
<a href="@category.Url">@category.Name</a> »
}
</div>
</div>
}
but I still need to cache the category path output on each product. so I created an action CategoryPath with OutputCache attribute enabled
<!-- in ProductList.cshtml -->
@foreach(var product in Model.Products ){
<div class="product">
<div>@product.Name</div>
<div class="category">
@Html.Action("CategoryPath", new { ProductID = product.ID })
</div>
</div>
}
But apparently this is not allowed. I got this error:
OutputCacheAttribute is not allowed on
child actions which are children of an
already cached child action.
I believe they have a good reason why they need to disallow this. I really want this kind of nested Output Caching.
Any idea for a workaround?
In you SQL.GetCategories method you can get all categories and cache it if not already in cache. And filter categories by productID using LINQ TO OBJECTS. In this way you do not hit db every time you need to find categories of a product.
Now you only use OutputCache on ProductList, and you have a pretty decent performing (partial)view.