开发者

Asp.net MVC Ajax form that return Json to a javascript method

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

return Json(new {Message = "Message!"},
                            JsonRequestBehav开发者_运维问答ior.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?


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>


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜