2011-12-09 27 views
0

我有jQuery中下面的代碼MVC jQuery的響應

 success: function(response) 
     {  alert(response.id); 

     } 

我的問題是,我該如何在.NET C#傳遞從一個ActionResult控制器的響應值,使jQuery的可以得到它?假設我想從控制器傳遞id值,以便jquery可以得到它。

回答

2

我假設你正在試圖調用從jQuery的一個動作,並取回了一定的成果......在這種情況下,你可以使用JsonResult:

public JsonResult Action(int id) 
{ 
    ... 
    return Json(new { id = id }); 
} 

然後response.id應該 「工作」。

+0

謝謝您的答覆。所以肯定需要成爲一個JsonResult vs只是一個regulre的ActionResult這個工作,對嗎? –

+0

是的,對於這種場景,您應該使用JsonResult/Json。 – lalibi

0

您可能需要做這樣的事情

success: function(response) 
     {  
      //this should turn the returned data into a json object 
      var obj = $.parseJson(response); 
      alert(obj.id); 
     } 
0

你可以嘗試這樣的

public ActionResult action(int id){ 

    string data= JsonConvert.SerializeObject(obj); // you can convert your object to json and then send to ajax call back 

    return ok(data); 
}