Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use html.beginform vs ajax.beginform

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?

like image 562
sagesky36 Avatar asked Oct 06 '13 18:10

sagesky36


People also ask

What is the difference between HTML BeginForm and ajax BeginForm?

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.

Why we use HTML BeginForm?

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.

What is HTML BeginForm ()?

"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.

Can we use multiple BeginForm in MVC?

Thanks for your help ! Multiple form tags should work fine in MVC unless they are nested.


2 Answers

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)

like image 190
A Khudairy Avatar answered Oct 07 '22 20:10

A Khudairy


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/

like image 23
Jatin patil Avatar answered Oct 07 '22 20:10

Jatin patil