2014-11-04 79 views
0

我有一個ActionResult,它調用一個API並返回數據,但它需要很多時間,所以我想顯示一個進度條,所以我做了一個Ajax調用另一個ActionResult並保持它們之間的數據使用會話,但它不適用於多個請求,也因爲會話鎖定導致延遲有更好的方法來做到這一點 我的第一個動作2正在執行像循環,我需要在動作1中的循環值顯示進度,並返回到調用AjaxMVC維護mvc中進度條的兩個動作之間的值

我的代碼是這樣的: -

setInterval(extendsession, 2000); 
    function extendsession() { 
     $.ajax({ 
      url: '@Url.Action("Action1", "Controller")', 
      type: 'get', 

      async:true, 
      success: function (result) { 
       $("#status").html(result); 
       var progress = []; var progressPercentage; 
       if (result != "") { 
        progress = result.split(","); 
        progressPercentage = (progress[1]); 
       } 

       $("#progressbar").progressbar({ 
        value: parseInt(progressPercentage) 
       }); 
      }, 

     }); 

和行動

public ActionResult Action2() 
     { 
while(true) 
do api call.. 
     Session["ProgressPercentage"] //sava data in session 
} 

//below action is called by ajax to get progress 
public string Action1() 
{ 
return Session["ProgressPercentage"].toString(); 
} 

回答

0

EDITED

public ActionResult acetion1() 
    { //Api call 
    TempData["Result"] = "ProgressPercentage"; 
    } 

public ActionResult action2() 
{ 
    var result= TempData["Result"]; 
    return result; 
} 
+0

沒有其實我已經得到的數據,並將其返回在阿賈克斯,所以我需要一個更好的方式來行動之間的第一共享,使用會話造成像會話鎖定等問題 – user3707167 2014-11-04 08:38:05

+0

檢查已更新的答案。 – Rushee 2014-11-04 09:34:37

+0

因爲我使用setInterval,臨時數據在這個多個請求中丟失; – user3707167 2014-11-04 09:46:04