在jQuery中将表单发送到服务器

I am learning ASP.NET MVC. I have to submit a to controller side after validation in client-side(in jquery). How this can be done? Should i use <form action="#" method="post"> instead of <form action="Controller/Method" method="post"> and add an event handler in click event of submit button of , to send via ajax etc? What should i do? pls help

You are on the right track, and what you suggested will work.

A better method would be to leave the original action intact, providing backwards compatibility to older browsers. You would then create the event handler as normal, and include code to prevent the default submit behavior, and use ajax instead.

$('#submitbutton').live('click', function(e){ e.preventDefault(); });

The easiest way to do this is to use the jQuery forms plugin.

This is my go-to plugin for this type of thing. Basically it will take your existing form, action url etc and convert the submission to an ajax call automatically. From the website:

The jQuery Form Plugin allows you to easily and unobtrusively upgrade HTML forms to use AJAX. The main methods, ajaxForm and ajaxSubmit, gather information from the form element to determine how to manage the submit process. Both of these methods support numerous options which allows you to have full control over how the data is submitted. It is extremely useful for sites hosted in low cost web hosting providers with limited features and functionality. Submitting a form with AJAX doesn't get any easier than this!

It will also degrade gracefully if, for some reason, javascript is disabled. Take a look at the website, there are a bunch of clear examples and demos.

This is how I do:

In jQuery:

$('document').ready(function() {
    $('input[name=submit]').click(function(e) {
        url = 'the link';
        var dataToBeSent = $("form#myForm").serialize();
        $.ajax({
            url : url,
            data : dataToBeSent,
            success : function(response) {
                alert('Success');
            },
            error : function(request, textStatus, errorThrown) {
                alert('Something bad happened');
            }
        });
        e.preventDefault();
    });

In the other page I get the variables and process them. My form is

<form name = "myForm" method = "post">//AJAX does the calling part so action is not needed.
    <input type = "text" name = "fname"/>
    <input type= "submit" name = "submit"/>
<FORM>

In the action page have something like this

name = Request.QueryString("fname")

UPDATE: As one of your comment in David's post, you are not sure how to send values of the form. Try the below function you will get a clear idea how this code works. serialize() method does the trick.

$('input[name=submit]').click(function(e){
    var dataToBeSent = $("form#myForm").serialize();
    alert(dataToBeSent);
    e.preventDefault();
})