使用 ASP.NET MVC 3 持续刷新 PartialView

发布于 2024-11-10 09:16:24 字数 712 浏览 0 评论 0原文

我正在使用 ASP.NET MVC 3 开发一个网站。

我有许多部分包含来自不同部门的新闻,如下图所示:

https://i.sstatic.net/G21qi.png

使用

@Html.Action("_News", "Home", new { id = 1 })

Where id 1 = "Ledelsen", 2 = "Omstillingen" 等将这些部分添加到母版页。

我的控制器包含以下操作:

[ChildActionOnly]
public ActionResult _News(int id)
{
    // controller logic to fetch correct data from database
    return PartialView();
}

我已启动并运行 CRUD,但我的问题是如何以设定的时间间隔刷新 PartialViews 而无需回发?

我猜我必须使用 Javascript/jQuery 来完成此任务,但我无法做到。

谁能指出我正确的方向,或者更好的是,提供一个如何做到这一点的例子?

提前致谢

编辑:只是为了澄清我不想刷新整个页面,而只对部分视图进行异步刷新

I am developing a website using ASP.NET MVC 3.

I have a number of sections containing news from different departments, as shown in the picture below:

https://i.sstatic.net/G21qi.png

The sections are added to the masterpage by using

@Html.Action("_News", "Home", new { id = 1 })

Where id 1 = "Ledelsen", 2 = "Omstillingen" and so on.

My Controller contains the following Action:

[ChildActionOnly]
public ActionResult _News(int id)
{
    // controller logic to fetch correct data from database
    return PartialView();
}

I got CRUD up and running, but my question is how I can refresh the PartialViews without postback, at a set interval?

I'm guessing I have to use Javascript/jQuery to accomplish this, but I have not been able to.

Can anyone point me in the right direction or better yet, provide an example of how to do this?

Thanks in advance

EDIT: Just to clarify I do not want the whole page to refresh, but only do an asynchronous refresh of the partialviews

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

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

发布评论

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

评论(2

海夕 2024-11-17 09:16:24

在您的部分视图中,我建议将整个内容包装在 div 中:

<div id="partial1">

在 javascript 中,您需要一个函数来使用 AJAX 将部分视图加载到 div 中:

$("#partial1").load("_News", { id="1"} );

这就是使用 jQuery。一些文档此处。基本上,这将替换由调用 _News 操作生成的视图指示的 div。如果您向 javascript 调用添加一些逻辑,则 id 可以改变。

然后将对 load() 的调用包装在 setTimeout() 中:

var t = setTimeout(function () {$("#partial1").load("_News", { id="1"} );}, 60000);

这将每 60 秒运行一次包含的函数。可以在 JavaScript 代码中的任何位置使用 t 来停止自动刷新。

编辑

这应该可以解决缓存问题。感谢@iko 的建议。我注意到您需要 cache: false 才能正常工作。

var t = setTimeout(function () { $.ajax({ 
                                     url: "_News", 
                                     data: { "id": "1" }, 
                                     type: "POST", 
                                     cache: false, 
                                     success: function (data) { 
                                          $("#partial1").innerHTML = data; 
                                     }}); }, 60000);

In your partial view, I'd suggest wrapping the whole thing in a div:

<div id="partial1">

In javascript you'll need a function to use AJAX to load the partial view into the div:

$("#partial1").load("_News", { id="1"} );

That's using jQuery. Some documentation here. Basically, that will replace the div indicated with view generated from the call to your _News action. The id can be varied if you add some logic to the javascript call.

Then wrap the call to load() in a setTimeout():

var t = setTimeout(function () {$("#partial1").load("_News", { id="1"} );}, 60000);

That will run the included function every 60 seconds. t can be used, thusly clearTimeout(t) anywhere in your javascript code to stop the auto refreshing.

EDIT

This should address the caching issue. Thanks @iko for the suggestion. I've noticed you need cache: false for this to work.

var t = setTimeout(function () { $.ajax({ 
                                     url: "_News", 
                                     data: { "id": "1" }, 
                                     type: "POST", 
                                     cache: false, 
                                     success: function (data) { 
                                          $("#partial1").innerHTML = data; 
                                     }}); }, 60000);
神回复 2024-11-17 09:16:24

我将向您推荐我对 Razor (mvc 3) 中的相关问题 "UpdatePanel" 给出的答案起点。请注意,这与史蒂夫·马洛里的答案没有太大不同,但强调了其他一些要点。

但不同之处在于,如果您想单独更新每个部分视图(仅更新 id 1 或仅更新 id 2),您可能需要删除 [ChildActionOnly] 属性。

该脚本可以放置在返回的部分中。

<script type="text/javascript">
    setInterval(function()
    {
          $.load('<%= Url.Action("_News", "Home", new { id = Model.Id } ) %>', function(data) {
          $('#partial_'@(Model.Id)').html(data);
      });
    }
    , 30000);
</script>

<div id="partial_@(Model.Id)" />

I'm going to refer you an answer I gave to a related question "UpdatePanel" in Razor (mvc 3) for a starting point. Note, that its not that different to Steve Mallory's answer but highlights some other points.

The difference however being, that you may need to remove the [ChildActionOnly] attribute if you want to update each partial view individually (update id 1 only or update id 2 only).

This script can be placed in the returned partial.

<script type="text/javascript">
    setInterval(function()
    {
          $.load('<%= Url.Action("_News", "Home", new { id = Model.Id } ) %>', function(data) {
          $('#partial_'@(Model.Id)').html(data);
      });
    }
    , 30000);
</script>

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