Nate Pet
Nate Pet

Reputation: 46222

MVC Jquery response

I have the following code in jquery

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

      } 

My question is, how do I pass a response value from an ActionResult Controller in .NET c# so that jQuery can get it? Say I wanted to pass the id value from the Controller so that jquery can get a hold of it.

Upvotes: 0

Views: 132

Answers (4)

JRA
JRA

Reputation: 487

You can try like this

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);
}

Upvotes: 0

afreeland
afreeland

Reputation: 3979

You may have to do something like this

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

Upvotes: 0

lalibi
lalibi

Reputation: 3078

I assume that you are trying to call an Action from jQuery and get back some results... In that case you can use JsonResult:

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

and then response.id should "work".

Upvotes: 2

Kamran Ali
Kamran Ali

Reputation: 5954

this articles discuss different response type from ActionResult Controller in .NET c# http://msdn.microsoft.com/en-us/library/dd410269.aspx

Upvotes: 0

Related Questions