开发者

mvc editor templates result is cached

I use editor templates with a custom master page so that

Html.EditorFor(o => o.Name)

generates a label and an input, I also use a custom DisplayName attribute to localize the labels

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

I've put a breakpoint in the attribute's constructor and noticed that it is called only the first time I render the page with the EditorFor on it, so I guess the result of the editorfor is cached开发者_开发知识库, anybody knows how to avoid this caching ?


Ideally you need to use [NoCache] attribute on the 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);
    }
}

It is also possible to use <%@ OutputCache %> directive in the template - but some complain that it does not always work.

For reference look here.

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


I have encountered same problem. This 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 ViewData.ModelState object and on unknown reasons ModelState cached after ajax Request.


I had a similar issue when deploying to an Azure Website. An old version of an EditorTemplate was being consistently shown. I tried manually Publishing the cshtml file, I tried FTPing into the site and deleting both the Views folder and the Bin folder; but still this ghost of an EditorTemplate was haunting the site!

What finally worked was checking the Remove additional files at destination option under Build > Publish > Settings > File Publish Options.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜