2010-04-27 18 views
3

我有一個在數據庫中保存一個對象ajax的形式,然後返回一個像這樣的消息:Asp.net MVC Ajax的形式返回JSON的JavaScript方法

return Json(new {Message = "Message!"}, 
          JsonRequestBehavior.AllowGet); 

我們確定這裏,但我不不知道如何在視圖中顯示此結果以顯示在jQuery模式中。我的阿賈克斯形式如下,我想在OnSuccess方法上得到結果:

<%using (Ajax.BeginForm("Form", "Controller", new AjaxOptions() { OnSuccess = "MethodThatIWantToGetTheJson" }))%> 

任何想法?

回答

5

試試這個(從How to use Ajax.BeginForm MVC helper with JSON result?拍攝):

<%using (Ajax.BeginForm("Form", "Controller", new AjaxOptions() { OnComplete = "MethodThatIWantToGetTheJson" })) 

<script type='text/javascript'> 
    function MethodThatIWantToGetTheJson(content) { 
     alert(content.get_response().get_object()); 
    } 
</script> 
+0

我沒有看到這個另外一個問題,但那非常有幫助!謝謝! – 2010-04-27 18:29:47

2

我將使用jQuery示例,因爲這是我通常使用ASP.NET MVC請求任何東西的方式。如果我們設置了ajax請求,我們會將響應作爲json返回。

$.ajax({ 
    url: 'Controller\Action\', 
    type: 'POST', 
    dataType: 'json' 
    success: function(data, status) 
    { 
     // data will be your json result 
     alert(data.Message); 
    } 
}); 

然後,您可以只將它放入某種jQuery的邏輯,就像這樣:

​​
相關問題