I am trying to understand the difference between JsonResult and Ajax.BeginForm work? Can someone be kind enough to give examples of each? Can the functionality of each be accomplished by JQuery? If so, how?
Thanks
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
JsonResultis just a kind ofActionResultderived class that indicates that this action will return JSON instead of a view or something else.For example:
This controller action, when invoked, returns the JSON serialized representation of the model:
In addition to that it sets the
Content-TypeHTTP response header toapplication/json.Ajax.BeginFormis an HTML helper used to generate a<form>element but which will be submitted using AJAX to the server. So if you point this form to the controller action returning JSON you will be able to retrieve the results of this JSON in the success callback. The results will be automatically parsed into a javascript object that you can access its properties.For example:
This will generate a
<form>element which will send an AJAX request when submitted to theFooaction. For this to work you need to include the following script to your page:Now all that’s left is to write this
onSuccessjavascript function and process the JSON results returned by the server:Behind the scenes when you include the
jquery.unobtrusive-ajax.js, all forms or links that were generated withAjax.*helpers will automatically be parsed and AJAXified with jQuery. The HTML5data-*attributes that those helpers generated will be processed and turned into AJAX calls.You could of course decide to use plain jQuery and none of the
Ajax.*helpers. In this case you don’t need including thejquery.unobtrusive-ajax.jsscript. You don’t need using any of the Ajax.* helpers.To generate the form you could use a normal
Html.BeginFormhelper:and then in a javascript file use jQuery to subscribe to the
.submitevent of this form, cancel the default synchronous submission by returning false and sending an AJAX request instead: