如何在 MVC 4 中每 3 秒刷新一次局部视图? [英] How do I refresh a partial view every 3 seconds in MVC 4?
问题描述
我需要根据已完成的作业数量获取进度条进行更新.完成的作业数存储在 SQL Server DB 的作业表中.我需要我的 ASP.NET MVC 4 应用程序每 3 秒查询一次数据库以获取竞争作业的数量,并使用此数据更新局部视图中的进度条.计时器工作,它在 StatusController 中调用我的 _getforStatus 操作,它似乎调用了 EntityDB,但似乎从未在计时器告诉它之前调用视图.如何让进度条每 3 秒刷新一次?
我的 _Layout.cshtml 视图的标题调用 StatusController 中的计时器的启动,如下所示:
...@Html.Action("InitTimer", '状态")...头部>
此外,在 _Layout 视图中,我将局部视图调用到 Jumbotron 中,如下所示:
@{Html.RenderAction("_GetforStatus", "状态");}
状态控制器的编码如下:
公共类 StatusController : 控制器{集成数据库实体_db;私人定时器 timer1;公共无效 initTimer(){定时器 1 = 新定时器();timer1.elapsed += new ElapsedEventhandler(timer_Elapsed(;timer1.interval = 3000;timer1.Start();}Private void timer_Elapsed(Object sender, EventArgs e){_GetforStatus();}[ChildActiononly]公共 PartialViewResult _GetforStatus(0{_db = 新的集成数据库实体();ViewDataModel - _db.Jobs.ToList();返回局部视图();}
我还尝试了以下 Ajax 代码.我没有出错,但我的进度条从未更新:
<脚本>函数 loadPartialView() {$.ajax({url: '@url.Action("_GetforStatus', "状态")",类型:'获取',数据类型:'html',成功:功能(结果){$('progress').html(result);}});}$函数(){加载部分视图();window.setInterval("loadPartialView()", 3000);});
我不确定它是否不起作用,因为我没有在 JS 中正确表示Html.RenderAction"或什么.
我认为您需要对 ajax 调用进行一些更改.这是我如何拨打电话的示例
$.ajax({url: '@Url.Action("Action", "Controller")',类型:'帖子',缓存:假,异步:真,数据:{ id:frmUser"},成功:功能(结果){$('.divPartial').html(结果);}});
在您的控制器上,我认为您不仅需要子操作.此外,您正在返回一个空的局部视图对象.应该是
return partialView('_partialName', Model);
最后在您的 jquery 中,要保持呼叫发生,您需要重新触发呼叫.尝试这样的事情
$(document).ready(function(){函数 RefreshPartial(){//这将等待 3 秒,然后触发加载部分函数设置超时(功能(){加载部分视图();//调用这个函数,让它继续循环刷新部分();}, 3000);}//初始化循环刷新部分();});
I need to get a progress bar to update based on the number of jobs completed. The number of jobs completed is stored on a jobs table of a SQL Server DB. I need my ASP.NET MVC 4 application to query the DB every 3 seconds for the number of jobs compete and use this data to update the progress bar which is held in a partial view. The timer works and it calls my the _getforStatus action in the StatusController, and it seems to call the EntityDB, but is never seems to call the view other than before the timer tells it to. How do I get the progress bar to refresh every 3 seconds?
The header of my _Layout.cshtml view calls the initiation of the timer which is in the StatusController like so:
<head>
...
@Html.Action("InitTimer", 'Status")
...
</head>
Also, in the _Layout view I call the partial view into a Jumbotron like so:
<div class="jumbotron" id=progress">
@{Html.RenderAction("_GetforStatus", "Status");}
</div>
The Status controller is coded like so:
public class StatusController : Controller
{
IntegrationDBEntities _db;
Private Timer timer1;
Public void initTimer()
{
timer1 = new Timer();
timer1.elapsed += new ElapsedEventhandler(timer_Elapsed(;
timer1.interval = 3000;
timer1.Start();
}
Private void timer_Elapsed(Object sender, EventArgs e)
{
_GetforStatus();
}
[ChildActiononly]
public PartialViewResult _GetforStatus(0
{
_db = new IntegrationDBEntities();
ViewDataModel - _db.Jobs.ToList();
return partialView();
}
EDIT: I also tried the following Ajax code. I got no error, but my progress bar never updated:
<script>
function loadPartialView() {
$.ajax({
url: '@url.Action("_GetforStatus', "Status")",
type: 'GET',
dataType: 'html',
success: function(result) {
$('progress').html(result);
}
});
}
$function () {
loadPartialView();
window.setInterval("loadPartialView()", 3000);
});
</script>
I'm not sure if it isn't working bc i haven't represented "Html.RenderAction" in JS correctly or what.
I think you need to make some changes to your ajax call. Here is an example of how I do the call
$.ajax({
url: '@Url.Action("Action", "Controller")',
type: 'post',
cache: false,
async: true,
data: { id: "frmUser" },
success: function(result){
$('.divPartial').html(result);
}
});
on your controller I don't think you need the child action only. Also you are returning an empty partial view object. It should be
return partialView('_partialName', Model);
finally in your jquery to keep the call happening you need to retrigger the call. Try something like this
$(document).ready(function(){
function RefreshPartial(){
//this will wait 3 seconds and then fire the load partial function
setTimeout(function(){
loadPartialView();
//recall this function so that it will continue to loop
RefreshPartial();
}, 3000);
}
//initialize the loop
RefreshPartial();
});
这篇关于如何在 MVC 4 中每 3 秒刷新一次局部视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!