views:

613

answers:

2

I have an Ajax.Action link hooked up to a post method in my controller class that returns a ContentResult. I'm able to make the request and get the response just fine when looking in a tool like firebug, but I'm having problems trying to actually access or do anything with the response text.

Basically, I have something like this in my controller:

public ContentResult RevertToDefault(int id, string default)
{
  /** Update the DB **/

  return Content(default);
}

And this in my view:

<%= Ajax.ActionLink(
  "Revert",
  "RevertToDefault",
  new { id = Model.MyObject.ID, default = Model.MyObject.DefaultValue },
  new AjaxOptions { OnComplete = "function(r) { alert(r); }" })%>

Right now, the alert just returns [object Object]. The change is made in the database, and I'm guessing that I have access to the ContentResult somewhere in that r object, but I'm having a hard time finding good examples/documentation online.

Any ideas?

+1  A: 

As far as I know the argument to the OnComplete event handler is an ajaxContext, so you could try ajaxContext.get_response() and get your response object or ajaxContext.get_data(), which should return the actual data sent as response.

emaster70
+1  A: 

Yes, the previous answer is absolutely correct. Just do this:

<%= Ajax.ActionLink(
  "Revert",
  "RevertToDefault",
  new { id = Model.MyObject.ID, default = Model.MyObject.DefaultValue },
  new AjaxOptions { OnComplete = "function(r) { alert(r.get_data()); }" })%>
Dmitry Perets