有幾種方法可以做到這裏面Controller
。起初,您應該知道,每次向控制器發送請求時,您都會使用不同的相同控制器的實例。
第一種方式:使用單身。將值存儲在公共靜態實例中,每次請求時加載一次並獲取相同的列表。但是,因爲它是數據庫值 - 您需要定期更新數據庫中存儲的值。
public class LoaderSingleton
{
private static readonly LoaderSingleton _instance = new LoaderSingleton();
private List<DataRow> _items;
static LoaderSingleton()
{
}
private LoaderSingleton()
{
}
public static LoaderSingleton Instance
{
get
{
return _instance;
}
}
public List<DataRow> Items
{
get
{
if (this._items == null)
{
this.Load();
}
return this._items;
}
}
public void Load()
{
this._items = //perform load from database
}
}
方式二:使用Session
變量。將值存儲在用戶會話中而不是靜態類中。你不應該忘記更新值和清理會話。
//inside controller
this.Session[str_salesClass] = SalesRepository.SalesCount();
第三條道路:使用ViewData
變量。值將僅存儲在請求之間,您應該在上次請求完成後使用新值更新值。
//inside controller
this.ViewData[str_salesClass] = SalesRepository.SalesCount();
第四種方式:執行從您的視圖async
請求。例如,您正在將所需的值加載到基本視圖,它可以異步加載另一個視圖,並且當您發送加載子視圖的請求時 - 您應該將加載的數據作爲參數傳遞。
public ActionResult BaseView()
{
ViewBag.Data = SalesRepository.SalesCount();
return View();
}
[ChildActionOnly]
public ActionResult ChildView1(List<DataRow> sales)
{
}
我不推薦使用Cache
,導致它有不可預知的行爲。當你不期望的時候,你會得到null
。
顯示你的代碼,所以我們可以SE你做了什麼至今 – Theunis
@VadimMartynov,謝謝瓦迪姆,我可能會想法有關緩存數據,但在我的情況,我怎麼能實現高速數據 – ronibd