如何在 MVC 4 中每 3 秒刷新一次局部视图?
How do I refresh a partial view every 3 seconds in MVC 4?
我需要一个进度条来根据完成的作业数进行更新。完成的作业数存储在 SQL 服务器数据库的作业 table 中。我需要我的 ASP.NET MVC 4 应用程序每 3 秒查询一次数据库以获取竞争的作业数,并使用此数据更新部分视图中保存的进度条。计时器工作,它在 StatusController 中调用我的 _getforStatus 操作,它似乎调用 EntityDB,但似乎从来没有调用视图,除非在计时器告诉它之前。如何让进度条每 3 秒刷新一次?
我的 _Layout.cshtml 视图的 header 调用 StatusController 中的计时器的启动,如下所示:
<head>
...
@Html.Action("InitTimer", 'Status")
...
</head>
此外,在 _Layout 视图中,我将局部视图调用为 Jumbotron,如下所示:
<div class="jumbotron" id=progress">
@{Html.RenderAction("_GetforStatus", "Status");}
</div>
Status 控制器的编码如下:
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();
}
编辑:
我还尝试了以下 Ajax 代码。我没有收到任何错误,但我的进度条从未更新:
<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>
我不确定它是否不起作用,因为我没有在 JS 中正确表示 "Html.RenderAction" 或其他什么。
我认为您需要对 ajax 调用进行一些更改。这是我如何调用的示例
$.ajax({
url: '@Url.Action("Action", "Controller")',
type: 'post',
cache: false,
async: true,
data: { id: "frmUser" },
success: function(result){
$('.divPartial').html(result);
}
});
在你的控制器上我不认为你只需要子动作。您还返回一个空的局部视图对象。应该是
return partialView('_partialName', Model);
最后,在您的 jquery 中,要继续通话,您需要重新触发通话。尝试这样的事情
$(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();
});
我需要一个进度条来根据完成的作业数进行更新。完成的作业数存储在 SQL 服务器数据库的作业 table 中。我需要我的 ASP.NET MVC 4 应用程序每 3 秒查询一次数据库以获取竞争的作业数,并使用此数据更新部分视图中保存的进度条。计时器工作,它在 StatusController 中调用我的 _getforStatus 操作,它似乎调用 EntityDB,但似乎从来没有调用视图,除非在计时器告诉它之前。如何让进度条每 3 秒刷新一次?
我的 _Layout.cshtml 视图的 header 调用 StatusController 中的计时器的启动,如下所示:
<head>
...
@Html.Action("InitTimer", 'Status")
...
</head>
此外,在 _Layout 视图中,我将局部视图调用为 Jumbotron,如下所示:
<div class="jumbotron" id=progress">
@{Html.RenderAction("_GetforStatus", "Status");}
</div>
Status 控制器的编码如下:
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();
}
编辑: 我还尝试了以下 Ajax 代码。我没有收到任何错误,但我的进度条从未更新:
<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>
我不确定它是否不起作用,因为我没有在 JS 中正确表示 "Html.RenderAction" 或其他什么。
我认为您需要对 ajax 调用进行一些更改。这是我如何调用的示例
$.ajax({
url: '@Url.Action("Action", "Controller")',
type: 'post',
cache: false,
async: true,
data: { id: "frmUser" },
success: function(result){
$('.divPartial').html(result);
}
});
在你的控制器上我不认为你只需要子动作。您还返回一个空的局部视图对象。应该是
return partialView('_partialName', Model);
最后,在您的 jquery 中,要继续通话,您需要重新触发通话。尝试这样的事情
$(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();
});