2012-07-30 30 views
0

我是新來的asp.net,我張貼我的所有數據到服務器使用$ .post(JQuery語法)。

控制器的動作被執行@最後我在完成動作方法執行後,將RedirectToAction調用到不同的動作方法。

執行達到已完成的$ .Post()的回調事件,其中im將請求的結果加載到頁面的根元素中。 $(HTML)的.html(結果)。

如何使用.post的$用RedirectToAction

回答

2

什麼是使用AJAX,當你更換整個頁面的意義呢? AJAX的全部重點只是刷新DOM的某個部分。如果你要刷新整個頁面,那麼只需使用標準鏈接,不需要AJAX。但是,如果我們假設你的控制器動作處理兩種情況:一個在它返回一個局部視圖,並在其中將其重定向,你可以通過目標URL爲JSON:

[HttpPost] 
public ActionResult SomeAction() 
{ 
    if (Something) 
    { 
     return PartialView(); 
    } 

    return Json(new { redirectTo = Url.Action("Foo", "Bar") }); 
} 

,然後在客戶端上:

$.post('@Url.Action("SomeAction")', function(result) { 
    if (result.redirectTo) { 
     // the controller action passed us the url to redirect to 
     window.location.href = result.redirectTo; 
    } else { 
     // the controller action passed us a partial result => 
     // let's update some portion of the DOM 
     $('#someId').html(result); 
    } 
}); 
+0

我們可以擺脫Window.Location.href在if塊,是否complusion完成回調方法在Jquery中啓動,我的問題是我們不能重定向到不同的視圖使用RedirectToAction使用$ .post – user145610 2012-07-31 03:06:04