2017-04-04 67 views
0

我试图从一个AJAX post响应的结果中的foreach函数获取原始计数。我面临的问题是,在我的帖子回复中,我收到了所有这些foreach的迭代的总数,而不是个人。这样做的目的是通过在每次迭代中填充进度条来显示上载的进度。如何检索控制器函数的计数到ajax进度条asp.net mvc

控制器:

public JsonResult progressFunction(int? SystemGeneralAnnouncementId) 
     { 

      var systemGeneralAnnouncement = (SystemGeneralAnnouncementId == null) ? null : _uow.SystemGeneralAnnouncementRepository.GetById(SystemGeneralAnnouncementId.Value); 
      List<Status> status = new List<Status>(); 

      if (systemGeneralAnnouncement.Statuses.Length > 0) 
      { 
       status.AddRange(systemGeneralAnnouncement.Statuses.Split(',').Select(item => (Status) Enum.Parse(typeof (Status), item))); 
      } 

      var allPocEmailAddresses = new List<InstitutionPointOfContact>(); 
      var pocEmailAddresses = new List<InstitutionPointOfContact>(); 

      //retrieve all Point of contact based upon selected statuses per each loop 
      var result = new List<InstitutionPointOfContact>(); 
      foreach (var item in status) 
      { 
       result = _uow.InstitutionPointOfContactRepository.GetAllByStatus(item).ToList(); 
       allPocEmailAddresses.AddRange(result); 
      } 

      // Retrieve the poc email addresses based on the who is intended to receive the email message 
      if (systemGeneralAnnouncement.SendToRecipients.Contains("(1) All Three POCs")) 
      { 
       pocEmailAddresses = allPocEmailAddresses; 
      } 
      else 
      { 
       if (systemGeneralAnnouncement.SendToRecipients.Contains("(2) All POCs")) 
       { 
        pocEmailAddresses.AddRange(allPocEmailAddresses.Where(r => r.PointOfContactType == PointOfContactTypes.Primary).ToList()); 
       } 
       if (systemGeneralAnnouncement.SendToRecipients.Contains("(3) All Compliance POCs")) 
       { 
        pocEmailAddresses.AddRange(allPocEmailAddresses.Where(r => r.PointOfContactType == PointOfContactTypes.Secondary).ToList()); 
       } 
       if (systemGeneralAnnouncement.SendToRecipients.Contains("(4) All Authorities")) 
       { 
        pocEmailAddresses.AddRange(allPocEmailAddresses.Where(r => r.PointOfContactType == PointOfContactTypes.SigningAuthority).ToList()); 
       } 
       if (systemGeneralAnnouncement.SendToRecipients.Contains("(5) All Rate POCs")) 
       { 
        pocEmailAddresses.AddRange(allPocEmailAddresses.Where(r => r.PointOfContactType == PointOfContactTypes.TuitionRates).ToList()); 
       } 
       if (systemGeneralAnnouncement.SendToRecipients.Contains("(6) Specified Email Address")) 
       { 
        var pocs = new List<InstitutionPointOfContact>(); 

        string[] emails = systemGeneralAnnouncement.EmailAddresses.Split(','); 

        foreach (string email in emails) 
        {      
         var addPoc = new InstitutionPointOfContact { Email = email }; 

         User user = _uow.UserRepository.GetByEmail(email); 

         if (user == null) 
         { 
          addPoc.FirstName = "Not Created Account Yet"; 
         } 
         else 
         { 
          addPoc.FirstName = user.FirstName; 
          addPoc.LastName = user.LastName; 
         } 

         List<InstitutionPointOfContact> opeidAssociatedToUser = 
          _uow.InstitutionPointOfContactRepository 
           .GetAllPocsByEmail(email) 
           .ToList(); 


         if (opeidAssociatedToUser.Count == 0) 
         { 
          addPoc.IDNumber = "N/A"; 
         } 
         else 
         { 
          string[] idArray = idAssociatedToUser 
           .Select(x => x.IDNumber) 
           .ToArray(); 

          addPoc.IDNumber = string.Join(",", opeidArray); 
         } 
         pocs.Add(addPoc); 
        }     
        pocEmailAddresses.AddRange(pocs); 
       } 
      } 

      ViewBag.UploadProgress = 0; 
      // if any poc addresses were found... 
      if (pocEmailAddresses.Count > 0) 
      { 
       string emailBody = WebUtility.HtmlDecode(systemGeneralAnnouncement.EmailBody); 

       foreach (InstitutionPointOfContact emailAddress in pocEmailAddresses.Where(x => x.Email != "" && x.Email != null).ToList()) 
       { 

        string firstName = emailAddress.FirstName == null ? "" : emailAddress.FirstName.Trim(); 
        string lastName = emailAddress.LastName == null ? "" : emailAddress.LastName.Trim(); 
        string userName = firstName + " " + lastName; 

        //iterative for progress bar 
        ViewBag.UploadProgress++; 

       } 
      } 

      return Json (ViewBag.UploadProgress, JsonRequestBehavior.AllowGet); 

     } 

AJAX:

$

(document).ready(function(){ 
    $.ajax({ 
     type: "POST", 
     url: "progressFunction", 
     cache: false, 
     cacheControl: "no-cache", 
     statusCode: { 
      500: function() { 
       errorWhileSavingData() 
      } 
     }, 
     success: function (data) { 
      alert() 
      GenerateProgressBar(data); 
      } 
     }); 
    }); 

我的问题是,我可以在后功能恢复的个体数从控制器(Viewbag.uploadProgress)等等我可以将它作为进度条中的变量计数传递给它?

更新:为了清楚起见,我需要做的是获得foreach(1〜n)的个人计数,而不是完成的计数,这正是我现在正在接受的计数。

更新2:SingalR是不是真的在这种情况下的选择,因为它是太过分了,这样一个小的过程,一个期望的结果将来自一个“滚你自己的”

+0

你为什么不返回实际的数字? –

+0

如果我正确地理解了这个问题,为什么我不把它作为原始值返回? – jpears

+0

@CristianSzpisjak我没有返回原始值的原因是因为最终用户不一定会理解上传进度,因为它在幕后发生。本质上,我们将选定的元素作为元值(所有POC,合规性,权限),然后将附加到这些值的数据库值传递给计划任务,稍后将其作为电子邮件发送(每周一次),因为我期望做一个进度条是需要3-5秒,我想告诉他们它正在进行并完成。 – jpears

回答

1

要回答你的问题,这根据您当前的设置是不可能的。使用ViewBag很好的尝试,但ViewBag实际上并没有在Controller和View之间传递,直到Action完成并返回到View。正如我所提到的,SignalR将是解决这个问题的方法之一,但对于您的使用情况来说是过分的。

+0

非常感谢您的澄清。我看到它会让我完成计数。有没有其他的方法可以用于你自己的方法? – jpears

+1

*咳嗽* SignalR :) – sleeyuen

+0

对不起,您的第一个陈述中已清楚。我的印象是我们正在讨论除信号员之外的其他东西。看起来我们将采取一种不同的方法,只需在ajax调用中加载一个轮子,但是您非常有帮助。谢谢。 – jpears

相关问题