爲了讓它不再需要JavaScript,可以將搜索分解爲多個操作。
第一個操作(/ Search /?q = whodunit)只是對您的參數進行一些驗證(因此您知道是否需要重新顯示錶單),然後返回一個使用元刷新指向的視圖瀏覽器回到「真正的」搜索行動。
你可以用兩個獨立的控制器動作實現這一點(比如搜索和結果):
public ActionResult Search(string q)
{
if (Validate(q))
{
string resultsUrl = Url.Action("Results", new { q = q });
return View("ResultsLoading", new ResultsLoadingModel(resultsUrl));
}
else
{
return ShowSearchForm(...);
}
}
bool Validate(string q)
{
// Validate
}
public ActionResult Results(string q)
{
if (Validate(q))
{
// Do Search and return View
}
else
{
return ShowSearchForm(...);
}
}
但據清爽去這給你一些障礙。因此,您可以將它們重新合併爲一個單獨的操作,該操作可以使用TempData以雙向過程的方式發出信號。
static string SearchLoadingPageSentKey = "Look at me, I'm a magic string!";
public ActionResult Search(string q)
{
if (Validate(q))
{
if (TempData[SearchLoadingPageSentKey]==null)
{
TempData[SearchLoadingPageSentKey] = true;
string resultsUrl = Url.Action("Search", new { q = q });
return View("ResultsLoading", new ResultsLoadingModel(resultsUrl));
}
else
{
// Do actual search here
return View("SearchResults", model);
}
}
else
{
return ShowSearchForm(...);
}
}
這包括點2,3,4,可以說是5
要包括支持#1意味着你要保存的搜索無論是在會議上,DB等結果
在這種情況下,只需將您所需的緩存工具欄添加爲「Do actual search here」位的一部分,並添加check-for-cached-result來繞過加載頁面。例如
if (TempData[SearchLoadingPageSentKey]==null)
成爲
if (TempData[SearchLeadingPageSentKey]==null && !SearchCache.ContainsKey(q))