Anwar Chandra
Anwar Chandra

Reputation: 8648

Nested partial output caching in ASP.NET MVC 3

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?

Upvotes: 4

Views: 3782

Answers (4)

swapneel
swapneel

Reputation: 3061

Use Child Action Cache For CategoryPath Action. There's also an example of ChildActionOnly in action.

Upvotes: 2

Flipster
Flipster

Reputation: 4401

We had a similar issue, and found that we didn't actually NEED nested partial output caching at all. Once we were caching the parent object, the child was not called again until the parent's cache expired.

So, my advice might be: Cache the parent and the child will already be handled.

Upvotes: 2

Nikki9696
Nikki9696

Reputation: 6348

I don't know much about MVC, but "nested caching" made me wonder why using varybyparam won't work here...

Upvotes: 0

Bcelik
Bcelik

Reputation: 1216

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.

Upvotes: 1

Related Questions