4

I have a jQuery $.post back to a MVC 4 Controller that will return back a PartialViewResult with rendered using the data sent in the POST. When debugging the Partial View and Controller, the correct data is being received and sent to the Partial View as the View Model. The issue is, when analyzing the HTML sent back in the AJAX result it is containing seemingly "cached" data from the original page refresh.

I have seen a good amount of posts on here that are similar, but none that were the same as my issue.

I am aware that HTTP Post requests do not cache in the browser, so that is not the issue. I also have set the set the OutputCache attribute to NoStore = true, etc.

Controller

[OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]
public partial class MyController : Controller
{ 
  ...
    [HttpPost]
    public virtual ActionResult UpdatePartial(MyViewModel myVm)
    {
        return this.PartialView("My/_Partial", myVm);
    }
}

JS

$('.someButton').click(function () {
        $.post(myAjaxUrl, $('form').serialize(), function (data) {
            $('#myContent').html(data);
        });
    });
Pat Burke
  • 580
  • 4
  • 13
  • Use Fiddler to catch the response and compare the response body with the ajax response in browser. – Corneliu Feb 14 '13 at 15:36
  • @Corneliu Thanks, they are the same. – Pat Burke Feb 14 '13 at 15:41
  • Add a cache buster param to your ajax url. While POSTs are supposedly not cached, I have seen it happen in some cases. – BNL Feb 14 '13 at 15:50
  • 1
    I'm able to work around this by adding ModelState.Clear prior to doing any operations on the model. `[HttpPost] public virtual ActionResult UpdatePartial(PersonViewModel model) { ModelState.Clear(); model.FirstName += "1"; model.LastName += "1"; model.Age += 1; return this.PartialView("../My/_Partial", model); }` – Joe Feb 14 '13 at 15:53
  • Also: http://stackoverflow.com/questions/1775170/asp-net-mvc-modelstate-clear - the answer by Tim Scott has more info and links. – Joe Feb 14 '13 at 15:57
  • 4
    This ASP MVC's default behavior, ASP MVC assumes that when you're rendering a view in response to a form POST that you're returning to the form because of a validation error. So the HTML helpers look at the values from `ModelState` before looking at the `Model` and repopulate the view with the previous data. Joe's workaround by clearing the ModelState with `ModelState.Clear();` is the best way to get around this. – Jack Feb 15 '13 at 23:29
  • @Joe That was it! Thanks! Post as an answer so i can mark as correct – Pat Burke Feb 17 '13 at 23:31

2 Answers2

2

I'm able to work around this by adding ModelState.Clear prior to doing any operations on the model.

[HttpPost] 
public virtual ActionResult UpdatePartial(PersonViewModel model) 
{ 
    ModelState.Clear(); 
    model.FirstName += "1"; 
    model.LastName += "1"; 
    model.Age += 1; 
    return this.PartialView("../My/_Partial", model); 
}

This question has an answer by Tim Scott with more info an links.

Community
  • 1
  • 1
Joe
  • 5,389
  • 7
  • 41
  • 63
0

By default JQuery will cache $.ajax XMLHttpRequests (unless the data type is script or jsonp). Since $.post is implemented via $.ajax, JQuery itself has cached your request. The following JS should work.

JS

$('.someButton').click(function () {
    $.ajax{(
        url: myAjaxUrl,
        data: myAjaxUrl,
        success: function (data) {
           $('#myContent').html(data);
        },
        cache: false
    });
});

You might also find it worthwhile to handle the error event in case the post doesn't succeed.

Alex Williams
  • 355
  • 1
  • 4
  • 2
    `By default JQuery will cache $.ajax XMLHttpRequests`. That's not true. The browser caches only GET requests. POST requests (which is what `$.post` is using) are never cached. – Darin Dimitrov Feb 16 '13 at 15:48
  • @DarinDimitrov how to force a `$.post` to cache the `HTTPPOST` Action? I have used the `OutputCache` attribute to decorate the action result and `cache:true` in jquery but still I cant get it to cache. – Rajshekar Reddy Oct 24 '16 at 07:43