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?
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With