0
votes

I have a partial view that is used in 2 places, one is using a view model within a post model with a manually added HtmlPrefix, so the field id looks like this:

id="Model_Address_AddressLine1"

In the other place I don't need a view model and the id is just "AddressLine1". It is the same partial view however.

However if i visit the page with "Model_Address_AddressLine1" first, IIS caches this partial view and then when I go to the other page it doesn't model bind and validate as the id is cached as "Model.Address.AddressLine1" and it doesn't pull "AddressLine1".

I have tried making the ajax call set to cache:false and set NoStore on the actions OutputCache attribute, but it still caches the partial views model ids and name, is there a way to stop it doing this?

Added this code if it helps, this is helper code that adds the prefix on one of the pages for the partial view, but not the other:

return helper.Partial(partialName, accessor.Compile().Invoke(helper.ViewData.Model),
                              new ViewDataDictionary(helper.ViewData)
                              {
                                  TemplateInfo = new TemplateInfo
                                  {
                                      HtmlFieldPrefix = prefix
                                  }
                              })

Response headers confirm caching:

Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,/;q=0.8 Accept-Encoding:gzip,deflate,sdch Accept-Language:en-US,en;q=0.8 Cache-Control:max-age=0 Connection:keep-alive Cookie:__RequestVerificationToken=qnKmH92lRoXCZWl_146N3uPiGRnWJCRZis6V_6cQ7mUJUdlVfAQVAxeTBtnhdTgM2s_NezoZghhwCrqBrv4GcjrCZr0XhG-6bHq7kfhlRrw1; ASP.NET_SessionId=dh225cux11sckpxl2uke30q3; FedAuth=77u/PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz48U2VjdXJpdHlDb250ZXh0VG9rZW4gcDE6SWQ9Il9iZTI5MDk4OC01ZjdjLTQyZTktYjMxOS0zOGJhYzM5OTBlMjctMzg3NTUxQjQyM0JENjFDQzIwMEQyQTc2MTRBQzBDMDYiIHhtbG5zOnAxPSJodHRwOi8vZG9jcy5vYXNpcy1vcGVuLm9yZy93c3MvMjAwNC8wMS9vYXNpcy0yMDA0MDEtd3NzLXdzc2VjdXJpdHktdXRpbGl0eS0xLjAueHNkIiB4bWxucz0iaHR0cDovL2RvY3Mub2FzaXMtb3Blbi5vcmcvd3Mtc3gvd3Mtc2VjdXJlY29udmVyc2F0aW9uLzIwMDUxMiI+PElkZW50aWZpZXI+dXJuOnV1aWQ6YTM1MWFmNjItYzU0Mi00MzQ2LWI2NzYtNGIyNjE0Y2YxMjU2PC9JZGVudGlmaWVyPjwvU2VjdXJpdHlDb250ZXh0VG9rZW4+ Host:127.0.0.1:444 User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.131 Safari/537.36 Response Headersview source Cache-Control:no-cache, no-store Content-Encoding:gzip Content-Length:9958 Content-Type:text/html; charset=utf-8 Date:Tue, 13 May 2014 08:15:52 GMT Expires:-1 Pragma:no-cache Vary:Accept-Encoding X-Frame-Options:SAMEORIGIN

1
Do you have the caching and Outputcache set to false on both parent methods that call the partialview? Have you also ensured you have any browser caching turned off?SkyBlues87
@SkyBlues87 They both call the same action in the same controller so yeh it was set for both. The browser doesn't cache it, because if I clear the browser cache, it still happens, IIS caches it, but can't switch it off.User101
Can you try NoCache attribute?Dmitry Zaets
Can you post the http response headers from the page requests - In chrome, F12, Network, select page after request and then response headers. I would be good to confirm the caching is working as required.SkyBlues87
@SkyBlues87 added aboveUser101

1 Answers

0
votes

If you will change RenderPartial to RenderAction - even the OutputCache will works fine for you. [OutputCache(Duration=0)]

I've used custom attribute for such purpose, it might be helpful for you too:

public class NoCache : ActionFilterAttribute
{
    public override void OnResultExecuting(ResultExecutingContext filterContext)
    {
        filterContext.HttpContext.Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1));
        filterContext.HttpContext.Response.Cache.SetValidUntilExpires(false);
        filterContext.HttpContext.Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
        filterContext.HttpContext.Response.Cache.SetCacheability(HttpCacheability.NoCache);
        filterContext.HttpContext.Response.Cache.SetNoStore();

        base.OnResultExecuting(filterContext);
    }
}

M.b. Phil Haack's article "Donut Hole Caching in ASP.NET MVC" will also help you?