Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC 2 disable cache for browser back button in partial views

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?

like image 953
Maksymilian Majer Avatar asked Nov 06 '22 13:11

Maksymilian Majer


2 Answers

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.

like image 122
Erik Funkenbusch Avatar answered Nov 11 '22 15:11

Erik Funkenbusch


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.

like image 44
SDReyes Avatar answered Nov 11 '22 16:11

SDReyes