javascriptasp.net-mvc

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


Solution

  • 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).