3

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?

0

2 Answers 2

5

Try this (taken from 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>
Sign up to request clarification or add additional context in comments.

1 Comment

I did not saw this another question, but that was VERY helpful! Thanks!
2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.