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

标签 asp.net-mvc-4 timer progress-bar entity-framework-6 asp.net-mvc-partialview

我需要根据已完成的作业数获取进度条以进行更新。完成的作业数存储在 SQL Server 数据库的作业表中。我需要我的 ASP.NET MVC 4 应用程序每 3 秒查询一次数据库以了解竞争的作业数量,并使用此数据更新部分 View 中保存的进度条。计时器工作,它在 StatusController 中调用我的 _getforStatus 操作,它似乎调用 EntityDB,但似乎从来没有调用 View ,除非在计时器告诉它之前。如何让进度条每 3 秒刷新一次?

我的 _Layout.cshtml View 的 header 调用 StatusController 中的计时器的启动,如下所示:

 <head>
      ...
       @Html.Action("InitTimer", 'Status")
      ...
 </head>

此外,在 _Layout View 中,我将部分 View 调用为 Jumbotron,如下所示:

 <div class="jumbotron" id=progress">
      @{Html.RenderAction("_GetforStatus", "Status");}
 </div>

Status Controller 的编码如下:

 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);
    } 
});

在你的 Controller 上我不认为你只需要子操作。您还返回一个空的局部 View 对象。应该是

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();
}); 

关于asp.net-mvc-4 - 如何在 MVC 4 中每 3 秒刷新一次局部 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28613630/

相关文章:

c# - 此类 Azure 网站挂起的原因可能是什么?

克隆线程上的 Linux 虚拟计时器行为

java - 从另一个类设置进度条

c# - 多个ProgressBar的多线程更新

jquery-ui - 我可以使用 jQuery UI 进度条来指示下载文件的进度吗?

c# - 在 Dropdownlist mvc4 中编辑和更新

asp.net-mvc-4 - ASP.Net MVC HotTowel 模板的用途是什么?

deployment - 如何使用 SQL Server CE 将 ASP.NET MVC4 应用程序部署到 Azure 网站

javascript - 使用jquery连续显示单词

python - 使用golang实现python的定时器