5

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?

Community
  • 1
  • 1
Maksymilian Majer
  • 2,956
  • 2
  • 29
  • 42

2 Answers2

1

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
  • 9,798
  • 16
  • 53
  • 92
  • What is wrong with this approach? it's recommended by Haacked itself http://haacked.com/archive/2009/05/12/donut-hole-caching.aspx. !? – SDReyes May 18 '10 at 15:14
  • The problem is that you are confusing the browser cache with server side caching. The asker is having problems with the browser cache. – Erik Funkenbusch Oct 07 '11 at 14:36
1

Unless you use an iframe or ajax, there is no way to disable the browser cache for only a part of the page. The browser just pulls the data back from it's cache, and either you disable the pages cache or not.

Erik Funkenbusch
  • 92,674
  • 28
  • 195
  • 291