Nested partial output caching in asp.net mvc 3
Posted
by
Anwar Chandra
on Stack Overflow
See other posts from Stack Overflow
or by Anwar Chandra
Published on 2010-12-27T21:09:23Z
Indexed on
2010/12/27
21:54 UTC
Read the original article
Hit count: 300
Hi All,
I am using Razor view engine in ASP.Net MVC 3 RC 2
this is part of my view city.cshtml
(drastically simplified for the sake of simple 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. but I really want this kind of nested Output Caching
Please, any idea for a workaround?
© Stack Overflow or respective owner