开发者

ASP.NET MVC: Caching combobox

Is it possible to cash drop down list?

I'm using a Telerik MVC Window, ComboBox, and the contents of the window (including ComboBox) is being returned with a partial view. Contents of the partial view depends on the list of parameters, but on the every div in this window there is a combo box contents of which is usually unchanged and it contains ~2000 recor开发者_高级运维ds.

i'm thinking about returning ViewData["ComboContent"] using separate controller with cashing before returning the window itself, but may be there is a better way...

TIA

updated (my controller code):

        [AcceptVerbs("GET")]
        [OutputCache(Duration = int.MaxValue, VaryByParam = "id")] //Some custom param??
        public ActionResult LoadTimeOffset(int id)
        {
            String error;
            IEnumerable<MyModel> model = repo.GetSomeVariableStuff(id, 10, out error); //always varies
            ViewData["ComboList"] = new SelectList(repo.GetComboitems(id), "Key", "Value", -1); //changes only on id

            if (model.Count() > 0)
            {
                return PartialView("Partial", model);
            }

            return Content(error);
        }


Cache the data instead of caching the drop-down.

So, instead of putting the SelectList into the ViewData, put the contents for it:

if (HttpContext.Current.Cache["ComboList"] == null)
{
    HttpContext.Current.Cache["ComboList"] = repo.GetComboitems(id); //use Add instead so that you can specify the cache duration.
}
ViewData["ComboList"] = HttpContext.Current.Cache["ComboList"]; //take from cache.

Note, code is not accurate, but it is an example only.

Then, in your view, render the combo.

I hope this helps.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜