1

我正在 ASP.Net 页面上进行高级规范,该页面可能会显示一些延迟的数据。

当页面加载时,呈现的初始数据将来自本地数据库(呈现速度很快)。我想要的是一个单独的过程来寻找更新的数据(来自我拥有的任何其他服务)。这更耗时,但想法是呈现数据,然后如果找到更新的数据,将其附加到现有页面的顶部。

我想就如何实现这一点提出一些建议。

其技术范围是 ASP.Net 4.0、C# MVC3 和 HTML5。

谢谢。

4

1 回答 1

2

使用 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
        }
    });
});

其中 Load 控制器操作将负责与远程服务通信并返回包含数据的部分视图:

public ActionResult Load()
{
    var model = ... go ahead and fetch the model from the remote service
    return PartialView(model);
}

现在,如果这种数据获取是 I/O 密集型的,您可以利用异步控制器和 I/O 完成端口,这将避免您在从远程源获取数据的冗长操作期间危及工作线程。

于 2011-04-14T16:39:08.080 回答