2013-07-01 16 views
0

我一直在潛伏大約一個小時試圖找到一個解決方案或模式(標題參考).. 讓我給你我的意思是一個例子:從服務器端發送到客戶端多個部分視圖異步延遲

Client-to-Server: Hey, I want information about "ww2" //using jquery's ajax .post() method 
Server-to-Client: Ok, got your request, prepare to receive data asynchronously. 
Server-to-Client: "World War II happen in 1939 to 1945" 
//after couple of seconds 
Server-to-Client: "The Alliance won the war." 
//some more delay 
Server-to-Client: "bla bla bla" 
Server-to-Client: "thats it, im done for now". 

現在很明顯,客戶端將顯示數據,只要它收到使用jquery。 我的主要問題是,我怎樣才能在服務器上的Action上調用HttpPOST,並異步返回許多PartialView

如果有任何其他方式/想法與樣品,它將不勝感激。

+1

使用Signalr .... – SLaks

+0

你正在寫它,就好像每一個已觸摸計算機必須知道任何種類的JavaScript庫的人.. 我會檢查出來,任何建議? – NucS

+1

http://signalr.net/不是 – SLaks

回答

1

使用SignalR是一種選擇

,但如果你想要做自己,而不是從Controller繼承你的控制器類,從AsyncController繼承它。 從AsyncController繼承的控制器可以處理異步請求,並且它們仍然可以服務同步操作方法。

public class HomeController : AsyncController 
    { 
     public void ExtensiveTaskActionAsync() 
     { 
      AsyncManager.OutstandingOperations.Increment(); 
      Task.Factory.StartNew(() => DoExtensiveTask()); 
     } 

     private void DoExtensiveTask() 
     { 
      Thread.Sleep(5000); // some task that could be extensive 
      AsyncManager.Parameters["message"] = "hello world"; 
      AsyncManager.OutstandingOperations.Decrement(); 
     } 


     public ActionResult ExtensiveTaskActionCompleted(string message) 
     { 
      //task complete so, return the result 
      return View(); 
     } 
    } 
相關問題