问题描述
|
我正在ASP.Net页上做一个高级规范,可能会显示一些延迟的数据。
页面加载时,显示的初始数据将来自本地数据库(显示速度很快)。我想要的是一个单独的过程,可以出去查找更新的数据(从我拥有的任何其他服务中)。这比较耗时,但是我们的想法是呈现数据,然后,如果找到较新的数据,则将其附加到现有页面的顶部。
我想要一些有关如何完成此操作的建议。
为此的技术范围是ASP.Net 4.0,C#MVC3和HTML5。
谢谢。
解决方法
带有jQuery的AJAX是实现此目标的好方法。因此,例如,您可以在标记上放置内容占位符div:
<div id=\"result\" data-remote-url=\"@Url.Action(\"Load\",\"SomeController\")\"></div>
然后在加载DOM之后:
$(function() {
$.ajax({
url: $(\'#result\').data(\'remote-url\'),type: \'POST\',beforeSend: function() {
// TODO: you could show an AJAX loading spinner
// to indicate to the user that there is an ongoing
// operation so that he doesn\'t run out of patience
},complete: function() {
// this will be executed no matter whether the AJAX request
// succeeds or fails => you could hide the spinner here
},success: function(result) {
// In case of success update the corresponding div with
// the results returned by the controller action
$(\'#result\').html(result);
},error: function() {
// something went wrong => inform the user
// in the gentler possible manner and remember
// that he spent some of his precious time waiting
// for those results
}
});
});
负载控制器操作将负责与远程服务进行通信,并返回包含数据的局部视图:
public ActionResult Load()
{
var model = ... go ahead and fetch the model from the remote service
return PartialView(model);
}
现在,如果这种数据获取是I / O密集型的,则可以利用异步控制器的I / O完成端口,这将避免您在从远程源获取数据的冗长操作期间危害工作线程。