In this article I will explain with an example, how to save Form data to Database using jQuery AJAX in ASP.Net MVC Razor.
When the Add Button is clicked, the InsertCustomer Action method is called using jQuery AJAX and the details of Customer are sent as JSON object.
Finally, after insertion the ID of the inserted Customer record is returned back to the View in ASP.Net MVC Razor.
Note: For beginners in ASP.Net MVC and jQuery AJAX, please refer my article: ASP.Net MVC: Call Controller Method from View using jQuery AJAX.
I have made use of the following table Customers with the schema as follows. CustomerId is an Auto-Increment (Identity) column.
ASP.Net MVC: Save Form data to Database using jQuery AJAX
I have already inserted few records in the table.
ASP.Net MVC: Save Form data to Database using jQuery AJAX
Note: You can download the database table SQL by clicking the download link below.
          Download SQL file
Entity Framework Model
Once the Entity Framework is configured and connected to the database table, the Model will look as shown below.
Note: For beginners in ASP.Net MVC and Entity Framework, please refer my article ASP.Net MVC: Simple Entity Framework Tutorial with example. It covers all the information needed for connecting and configuring Entity Framework.
ASP.Net MVC: Save Form data to Database using jQuery AJAX
The Controller consists of the following Action methods.
Action method for handling GET operation
Inside this Action method, simply the View is returned.
Action method for Inserting data to Database
Inside this Action method, the received Customer object is inserted into the Customers table and the updated Customer object with generated Customer Id is returned back to the calling jQuery AJAX function.
public class HomeController : Controller
    // GET: Home
    public ActionResult Index()
        return View();
    public ActionResult InsertCustomer(Customer _customer)
        using (CustomersEntities entities = new CustomersEntities())
        return Json(_customer);
The View consists of two TextBoxes and a Button. The Button has been assigned with a jQuery Click event handler.
When the Add button is clicked, the Name and the Country values are fetched from their respective TextBoxes and then passed to the InsertCustomer Action method using jQuery AJAX function.
The ID of the Inserted record is displayed using JavaScript Alert Message Box.
    Layout = null;
<!DOCTYPE html>
    <meta name="viewport" content="width=device-width"/>
    <table border="0" cellpadding="0" cellspacing="0">
            <td style="width: 150px">
                <input type="text" id="txtName" style="width:140px"/>
            <td style="width: 150px">
                <input type="text" id="txtCountry" style="width:140px"/>
            <td style="width: 200px">
                <input type="button" id="btnAdd" value="Add"/>
    <script type="text/javascript" src=""></script>
    <script type="text/javascript" src=""></script>
    <script type="text/javascript">
        $("body").on("click", "#btnAdd", function () {
            var txtName = $("#txtName");
            var txtCountry = $("#txtCountry");
            var _customer = {};
            _customer.Name = txtName.val();
            _customer.Country = txtCountry.val();
                type: "POST",
                url: "/Home/InsertCustomer",
                data: JSON.stringify(_customer),
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (r) {
                    alert("CustomerId: " + r.CustomerId.toString());
ASP.Net MVC: Save Form data to Database using jQuery AJAX