2017-04-04 20 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

相關問題