如何在MVC 4中每3秒刷新局部视图? [英] How do I refresh a partial view every 3 seconds in MVC 4?
问题描述
我需要根据完成的作业数量获取进度条来进行更新。完成的作业数存储在SQL Server数据库的作业表上。我需要我的ASP.NET MVC 4应用程序来查询数据库每3秒钟的作业数量竞争,并使用这些数据来更新在部分视图中保存的进度条。计时器工作,并且在StatusController中调用了我的_getforStatus操作,并且它似乎调用了EntityDB,但除了计时器告诉它之外,似乎没有调用视图。如何使进度条每3秒刷新一次?
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?
我的_Layout.cshtml视图的标题调用了StatusController中定时器的启动,如下所示:
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>
另外,在_Layout视图中,我将部分视图称为Jumbotron,如下所示:
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();
}
编辑:
我也尝试过以下Ajax代码,我没有错误,但我的进度条从来没有更新过:
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>
我不知道如果它不工作bc我没有代表Html.RenderAction 在JS正确或什么。
I'm not sure if it isn't working bc i haven't represented "Html.RenderAction" in JS correctly or what.
推荐答案
我想您需要对ajax调用进行一些更改。以下是我如何进行调用的示例:
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);
最后在你的jquery中保持呼叫发生,你需要重新触发呼叫。尝试这样的东西
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屋!