简体   繁体   中英

C# Using OutputCache in MVC3 Project

I am using MCV3 OutputCache to decrease the loading times of a page with a table full of data. I use ajax methods to update information and manipulate the DOM to show the user that their change has been succesful. This is fine until they load the page and the cached dataset is loaded instead of the updated one.

When the an Update method is called I would like to clear the cache or remove it, so that it is recreated on reload of the page, with the new updated data.

My code is as follows:

[OutputCache(CacheProfile = "VideoIndexView")]
public ActionResult Index()
{
    ...
    return View(model);
}

当您想从缓存中清除一些URL时,可以调用RemoveOutputCacheItem静态方法。

You could use your Index action result to load a template of the screen and use AJAX to get and load the actual data.

[OutputCache(CacheProfile = "VideoIndexView")]
public ActionResult Index()
{
    ...
    return View(model);  // Really only return a model that is okay to be cached
}

public ActionResult LoadData ()
{
    var Result = // Load the data
    ...
    return Json(Result);  // Don't forget to allow GET here if you're using HTTPGET
}

// Or...

public ActionResult LoadData ()
{
    var Result = // Load the data
    ...
    return PartialView (Result);
}

This way, Index can be cached just fine and the data will be loaded and injected into the page after the page has been served to the user. If you are going to use something like jQuery, be sure to tell it not to use cached results if you're using GET.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM