You should use Cache or OutputCache , put this list in Partial View , and then render it wherever you need:
1) Create an Action to plunge into Partial View . This view will be cached for maximum time, then any access will not create any overhead:
[NonAction] [OutputCache(Duration = int.MaxValue, VaryByParam = "none")] public ActionResult GetCourses() { List<Course> courses = new List<Course>(); return PartialView("_Courses", courses); }
2) Using Chache , filling out the Partial View in the same way:
[NonAction] public ActionResult GetCourses() { List<Course> courses = new List<Course>(); if (this.HttpContext.Cache["courses"] == null) { this.HttpContext.Cache["courses"] = courses; } else { courses = (List<Course>)this.HttpContext.Cache["courses"]; } return PartialView("_Courses", courses); }
3) Mark this view on Html.Action or Html.RenderAction :
@Html.Action("GetCourses", "ControllerName")
or
@{ Html.RenderAction("GetCourses", "ControllerName"); }
Additional Caching Information: Improving Performance with Output Cache
Fals source share