The result of the mvc editor template is cached

I use editor templates with a custom homepage to

Html.EditorFor(o => o.Name) 

generates label and input, I also use my own DisplayName attribute to localize shortcuts

 [DisplayNameLocalized("Name")] public string Name {get;set;} 

I set a breakpoint in the attribute constructor and noticed that it is only called the first time I process the page using EditorFor, so I assume that the result of the editor is encrypted, does anyone know how to avoid this caching?

+4
source share
3 answers

I ran into the same problem. It works for me

 public ActionResult Index(int? pageNumber) { var wishlistModel = new WishlistModel(); BindGifts(wishlistModel, pageNumber); if (Request.IsAjaxRequest()) { ViewData.ModelState.Clear(); return PartialView("_UserGiftList", wishlistModel); } return View(wishlistModel); } 

After some digging in MVC sources, I found that all Html helpers get data from the ViewData.ModelState object and for unknown reasons ModelState is cached after an ajax request.

+2
source

Ideally, you need to use the [NoCache] attribute in action.

 public class NoCacheAttribute : 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); } } 

You can also use the <%@ OutputCache %> directive in the template, but some complain that it does not always work.

For reference look here .

You can use <%@ OutputCache NoStore="true" %>

+4
source

I had a similar issue when deploying to Azure Website. An old version of EditorTemplate was shown sequentially. I tried to manually publish the cshtml file, I tried the FTP server on the site and deleted both the Views folder and the Bin folder; but still this ghost EditorTemplate haunted the site!

As a result, the check Delete additional files at destination in the Build> Publish> Settings> File Publish Settings section worked .

0
source

Source: https://habr.com/ru/post/1343003/


All Articles