如果您希望页面立即呈现并显示,然后稍后会发生某些事情(比如操作需要5秒钟才能完成),那么您可以通过执行此类操作获得数据可用时更新页面(有关
.getJSON的更多信息) ).
在Razor视图中,您可以添加一些JavaScript:
$.getJSON('LongRunningAsyncTask', function (result)
{
// Populate table or other actions with: result.Data
// ...
});
然后在你的控制器中你将有这样的任务:
public JsonResult LongRunningAsyncTask()
{
// Show this is async and won't render straight away...
System.Threading.Thread.Sleep(5000);
// Build up our view model
var viewModel = new TestViewModel();
// Send back as a Json result
var result = new JsonResult();
result.Data = viewModel;
return Json(result, JsonRequestBehavior.AllowGet);
}