views:

543

answers:

1

I am using Html.RenderAction<CartController>(c => c.Show()); on my master Page to display the cart for all pages. The problem is when I add an item to the cart and then hit the browser back button. It shows the old cart (from Cache) until I hit the refresh button or navigate to another page.

I've tried this and it works perfectly but it disables the Cache globally for the whole page an for all pages in my site (since this Action method is used on the master page). I need to enable cache for several other partial views (action methods) for performance reasons.

I wouldn't like to use client side script with AJAX to refresh the cart (and login view) on page load - but that's the only solution I can think of right now.

Does anyone know better?

A: 

Donut Hole Caching in ASP.NET MVC

If you want to cache all your page except the cart. You could implement a view control that contains the cart. and remove the cache policy from this view control.

<%@ Control Language="C#" Inherits="ViewUserControl<IEnumerable<Joke>>" %>
<%@ OutputCache Duration="100" VaryByParam="none" %>

<ul>
<% foreach(var joke in Model) { %>
    <li><%= Html.Encode(joke.Title) %></li>
<% } %>
</ul>

Haacked explains it in further detail here.

Hope it helps you.

SDReyes
What is wrong with this approach? it's recommended by Haacked itself http://haacked.com/archive/2009/05/12/donut-hole-caching.aspx. !?
SDReyes