加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc-4 – 如何在MVC 4中每3秒刷新一次局部视图?

发布时间:2020-12-16 00:10:03 所属栏目:asp.Net 来源:网络整理
导读:我需要根据已完成的作业数量来更新进度条.完成的作业数存储在SQL Server DB的作业表中.我需要我的ASP.NET MVC 4应用程序每隔3秒查询数据库以查找竞争数量,并使用此数据更新部分视图中保存的进度条.计时器工作,并在StatusController中调用我的_getforStatus操
我需要根据已完成的作业数量来更新进度条.完成的作业数存储在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();
});

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读