Asp.net MVC Ajax form that returns Json in javascript method

I have an ajax form that saves an object in a database and then returns a message like this:

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

We are fine, but I don’t know HOW I will get this result in the view for display in the jQuery module. My ajax form is as follows, and I want to get the result using the OnSuccess method:

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

Any ideas?

+3
source share
2 answers

Try this (taken from How to use the MVC Ajax.BeginForm helper with a 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>
+5
source

jQuery, -, ASP.NET MVC. ajax, json.

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

- jQuery, :

var message = $('<span/>');
message.html(data.Message);
message.dialog();
+2

Source: https://habr.com/ru/post/1743035/


All Articles