2010-04-16 113 views
0

我想完成以下任務:從另一個控制器操作中調用AsyncController操作?

class SearchController : AsyncController 
{ 
    public ActionResult Index(string query) 
    { 
     if(!isCached(query)) 
     { 
      // here I want to asynchronously invoke the Search action 
     } 
     else 
     { 
      ViewData["results"] = Cache.Get("results"); 
     } 

     return View(); 
    } 

    public void SearchAsync() 
    { 
     // some work 

     Cache.Add("results", result); 
    } 
} 

我打算讓來自客戶端的AJAX「平」,以便知道什麼時候結果出來,然後顯示出來。

但我不知道如何以異步的方式調用異步操作!

非常感謝。 路易斯

回答

0

你可以在一個新的線程調用操作:

if(!isCached(query)) 
{ 
    new Thread(SearchAsync).Start(); 
} 

的觀點可以使用AJAX調用到行動,將檢查結果出來:

public ActionResult Done(string query) 
{ 
    return Json(new 
    { 
     isDone = !isCached(query), 
     result = Cache.Get(query) 
    }); 
} 

而且ping:

var intId = setInterval(function() { 
    $.getJSON('/search/done', { query: 'some query' }, function(json) { 
     if (json.isDone) { 
      clearInterval(intId); 
      // TODO : exploit json.result 
     } else { 
      // TODO: tell the user to wait :-) 
     } 
    }); 
}, 2000); 
+0

謝謝!很酷的解決方案:) – Luis 2010-04-21 19:20:18

相關問題