views:

192

answers:

2

Hello guys, I have an ajax form that saves a object in the database then return a Message like this:

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

We are ok here, but I don't know HOW I'll get this result in the view to display in a jQuery modal. My ajax form is like the following and I want to get the result on the OnSuccess method:

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

Any ideas?

A: 

I'll use a jQuery example, because that's how I normally request anything using ASP.NET MVC. If we set up the ajax request, we get the response back as json.

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

You could then just put that into some kind of jQuery logic like so:

var message = $('<span/>');
message.html(data.Message);
message.dialog();
Tejs
A: 

Try this (taken from http://stackoverflow.com/questions/304233/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>
LukLed
I did not saw this another question, but that was VERY helpful! Thanks!
Diego Correa