为了让它不再需要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))