使用 ASP.NET MVC 3 持续刷新 PartialView
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在您的部分视图中,我建议将整个内容包装在 div 中:
在 javascript 中,您需要一个函数来使用 AJAX 将部分视图加载到 div 中:
这就是使用 jQuery。一些文档此处。基本上,这将替换由调用 _News 操作生成的视图指示的 div。如果您向 javascript 调用添加一些逻辑,则 id 可以改变。
然后将对
load()
的调用包装在setTimeout()
中:这将每 60 秒运行一次包含的函数。可以在 JavaScript 代码中的任何位置使用
t
来停止自动刷新。编辑
这应该可以解决缓存问题。感谢@iko 的建议。我注意到您需要
cache: false
才能正常工作。In your partial view, I'd suggest wrapping the whole thing in a div:
In javascript you'll need a function to use AJAX to load the partial view into the div:
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 asetTimeout()
:That will run the included function every 60 seconds.
t
can be used, thuslyclearTimeout(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.我将向您推荐我对 Razor (mvc 3) 中的相关问题 "UpdatePanel" 给出的答案起点。请注意,这与史蒂夫·马洛里的答案没有太大不同,但强调了其他一些要点。
但不同之处在于,如果您想单独更新每个部分视图(仅更新 id 1 或仅更新 id 2),您可能需要删除
[ChildActionOnly]
属性。该脚本可以放置在返回的部分中。
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.