If you are a programmer and have started learning Asp.NET MVC using C#, there would be several occasion when you would like to send form data without loading(refreshing) the page again.
In that case, you can have one option is to send form data using jQuery Ajax(which is not secure and not recommended to use) and second(recommended way) is to use Razor Ajax helper Ajax.BeginForm, before we begin you must have basic knowledge of Asp.NET MVC.
What is Ajax.BeginForm and how to use it?
Ajax.BeginForm is the extension method of the ASP.NET MVC Ajax helper class, with the help of which you can send data to the controller without loading your page, hence making your web-application more user -friendly.
To work Ajax.BeginForm functionality properly, we need to add the reference of jquery.unobtrusive-ajax library.
The Ajax.BeginForm takes the following parameters
actionName: parameter defines the
ControllerName: used to specify
routeValues: you can send route values using this field(if needed), Can be properly defined as, an object that contains the parameters for a route. The parameters are retrieved through reflection by examining the properties of the object. This object is typically created by using object initializer syntax.
- Url: Gets or sets the URL to make the request to.
- HttpMethod: used to define the form submit method such as POST, GET
- Confirm: Used to show the message to display in a confirmation window before a request is submitted.
- UpdateTargetId: Gets or sets the ID of the DOM element to update by using the response from the server, such as, if we specify the DIV tag id then only that particular DIV portion will get updated.
- InsertionMode: This property is used to specify the how the response will be inserted into the DOM element. It has InsertAfter, InsertBefore and
- AllowCache: boolean property which decides whether to allow cache or not.
- LoadingElementId: Gets or sets a value, in milliseconds, that controls the duration of the animation when showing or hiding the loading element.
- LoadingElementDuration: This property is used to define the duration in mileSeconds for loading symbol.
Ajax.Beginform Example :
Note: You need to install jquery.unobtrusive-ajax library before using Ajax.BeginForm. You can install it in your Solution using the package manager console and below code:
Install-Package Microsoft.jQuery.Unobtrusive.Ajax -Version 3.2.3
Usage Of Ajax.BeginForm in Real World applications:
- Use Ajax.Beginform to Update shopping cart in your Asp.Net MVC e-commerce project.
- Use it when you want to validate/submit form in Bootstrap Pop-up modal.
- It can be used when you have more than two forms in single page and you want to submit data to controller without reloading the complete page.
- In single page application(SPA) to collect users data/contact details
That's it, please share your comment using Facebook comments section or login/register to provide your valuable comments on this post or ask a question related to this post or MVC C#, thank you.
You might also like to read: