我需要根据已完成的作业数量来更新进度条.完成的作业数存储在sql Server DB的作业表中.我需要我的ASP.NET MVC 4应用程序每隔3秒查询数据库以查找竞争数量,并使用此数据更新部分视图中保存的进度条.计时器工作,并在StatusController中调用我的_getforStatus操作,它似乎调用EntityDB,但似乎永远不会在计时器告诉之前调用视图.如何让进度条每3秒刷新一次?
我的_Layout.cshtml视图的标题调用了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>
我不确定它是否不起作用bc我没有在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();
- });