Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.Net MVC, Submit a form using javascript

I have a form on one of my ASP.Net MVC views that I created using the following code

 <% using (Html.BeginForm(null, null, FormMethod.Post)) 

Using this code I have no control as far as I am aware of setting the name of the form. I'm now trying to write a javascript function to submit the form, is this possible without knowing the forms name?

Thanks

like image 377
Gavin Avatar asked Jun 07 '09 16:06

Gavin


People also ask

Can I use JavaScript to submit a form?

The JavaScript form submission can be used for object creation and various attributes can also be used. The attributes can be class, id, tag, etc. Calling by attributes is quite simple, we just need to place the symbols properly.

What is correct way to submit a form using JavaScript?

When we click on the link, the function submitForm() will get executed. This function will get the element object using DOM getElementById() method by passing the form id to this method, then the form will be submitted by using submit() method.

Can we use JavaScript in ASP NET MVC?

JavaScript can be used in asp.net mvc. If you go for Asp.NET Web forms, there is no need to have a detailed understanding of HTML, CSS or JavaScript as it abstracts all these details and provides automatic plumbing.


1 Answers

You can use jquery to submit the form:

<% using (Html.BeginForm(null, null, FormMethod.Post, new { id = "myForm"})) { %> 

(the last part is for htmlAttributes parameter)

just do this:

$("#myForm").submit(); 

and do not forget to include jquery-1.2.6.js that comes with mvc (or use a higher version).

like image 82
Serhat Ozgel Avatar answered Sep 19 '22 04:09

Serhat Ozgel