It's amazing after all the posts I've checked that there is still no definitive explanation (in my mind) in what situation should the subject data be used...
I know for the html.beginform, it will perform a postback, post data to a controller method, and either redirect to another method or return the same view to the user.
I know for the ajax.beginform, you must (correct me if I'm wrong) specify an UpdateTargetID where the resulting posted data from the controller method will go into something like a partial view within a div tag on the same page as the form. I know that you cannot redirect to another action method after the form is submitted.
Under both these conditions, you can still have the user input another round of data to submit and process via the controller.
So, unless you need to redirect to another action method, why wouldn't you use the ajax.beginform all of the time?
The only thing I can imagine, is that the html.BeginForm method would be probably best suited for data entry input over and over again whereas the ajax.beginForm method would be used to display a result to the user depending on what information they input into the form (almost like a one-time) deal. btw, I know I've contradicted myself with the use of saying to use the Ajax.BeginForm most of the time.
Can somebody please give me a relatively simple explanation when each of these methods should be used?
Html. BeginForm() will create a form on the page that submits its values to the server as a synchronous HTTP request, refreshing the entire page in the process. Ajax. BeginForm() creates a form that submits its values using an asynchronous ajax request.
Html. BeginForm is the Html Helper Extension Method that is used for creating and rendering the form in HTML. This method makes your job easier in creating form. Here, is the method to create a form using Html.
"BeginForm()" is an extension method that writes an opening "<form>" tag to the response. "BeginForm()" is an extension method for both HtmlHelper and AjaxHelper classes.
Thanks for your help ! Multiple form tags should work fine in MVC unless they are nested.
Well as you said, ajax calls are for when you need to stay in the same page, and it is mostly used in single page applications. (like when you submit an answer here on stackoverflow, it is just a partial refresh to the page)
But normally you want to go to another view (page). (like if your on some site doing a registration as a new user, after you submit you are redirected to the home page)
In Ajax forms, forms are submitted asynchronously using Javascript
.
Ajax forms are suitable in situations, where you need to do modify or save operations asynchronously
, without redirecting to any other forms.
For more info:
http://www.c-sharpcorner.com/UploadFile/3d39b4/working-with-html-beginform-and-ajax-beginform-in-mvc-3/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With