If we are using ajax call to return partial view, then response from the action method may return cached output from partial view. The issue has been discussed on several SO post here, here, here
To resolve issue i have to do ModelState.Clear()
in action method.
I have several action methods that returns partial view using Ajax call. So i thought instead of doing ModelState.Clear() in each method i can create custom ActionFilterAttribute
like below
public class NoCacheAttribute : ActionFilterAttribute
{
public override void OnResultExecuting(ResultExecutingContext filterContext)
{
filterContext.Controller.ViewData.ModelState.Clear();
base.OnResultExecuting(filterContext);
}
}
and then register it with global filters
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new NoCacheAttribute());
}
This seems to be working fine.
However I wanted to know if there is there any undesirable effect doing ModelState.Clear()
before action executing? If not, then why that is not a default behavior in asp.net mvc