将新数据加载到页面上而无需重新加载

发布于 2024-11-01 08:10:10 字数 248 浏览 0 评论 0原文

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

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

我想要一些关于如何实现这一目标的建议。

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

谢谢。

I'm doing a high-level spec on an ASP.Net page which may have some delayed data presented.

When the page loads, the initial data presented will originate from a local database (which will be fast in presenting). What I want is a separate process to go out and look for updated data (from whatever other services I have). This is more time consuming, but the idea is to present data, then if newer data is found, append this, on the fly to the top of the existing page.

I would like some recommendations on how to accomplish this.

The tech scope for this is ASP.Net 4.0, C# MVC3 and HTML5.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

漫漫岁月 2024-11-08 08:10:10

AJAX with jQuery 是实现这一目标的好方法。例如,您可以在标记上放置一个内容占位符 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 完成端口,这将避免您在从远程源获取数据的漫长操作期间危及工作线程。

AJAX with jQuery is a good way to achieve this. So for example you could put a content placeholder div on your markup:

<div id="result" data-remote-url="@Url.Action("Load", "SomeController")"></div>

and then once the DOM is loaded:

$(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
        }
    });
});

where the Load controller action will take care of communicating with the remote services and return a partial view containing the data:

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

Now if this fetching of data is I/O intensive you could take advantage of asynchronous controllers an I/O Completion Ports which will avoid you jeopardizing worker threads during the lengthy operation of fetching data from a remote source.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文