Cache
またはを使用OutputCache
し、このリストを に入れ、Partial View
必要な場所にレンダリングする必要があります。
1) を作成してAction
を作成しますPartial View
。このビューは最大期間キャッシュされ、アクセスしてもオーバーヘッドは発生しません。
[NonAction]
[OutputCache(Duration = int.MaxValue, VaryByParam = "none")]
public ActionResult GetCourses()
{
List<Course> courses = new List<Course>();
/*Read DB here and populate the list*/
return PartialView("_Courses", courses);
}
2)同じ方法でのChache
入力を使用:Partial View
[NonAction]
public ActionResult GetCourses()
{
List<Course> courses = new List<Course>();
if (this.HttpContext.Cache["courses"] == null)
{
/*Read DB here and populate the list*/
this.HttpContext.Cache["courses"] = courses;
}
else
{
courses = (List<Course>)this.HttpContext.Cache["courses"];
}
return PartialView("_Courses", courses);
}
Html.Action
3)またはでこのビューをレンダリングしますHtml.RenderAction
。
@Html.Action("GetCourses", "ControllerName")
また
@{ Html.RenderAction("GetCourses", "ControllerName"); }
キャッシュの詳細:出力キャッシュによるパフォーマンスの向上