In this article I will explain with an example, how to dynamically add rows to HTML Table on Button click using jQuery AJAX in ASP.Net MVC Razor.
Finally, the dynamically added rows of the HTML Table will be sent to Controller’s Action method using jQuery AJAX and later inserted in database using Entity Framework in ASP.Net MVC Razor.
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.
I have made use of the following table Customers with the schema as follows. CustomerId is an Auto-Increment (Identity) column.
Dynamically add rows to HTML Table in ASP.Net MVC
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.
Dynamically add rows to HTML Table in ASP.Net MVC
The Controller consists of two Action methods.
Action method for handling GET operation
Inside this Action method, all the records from the Customers table is returned to the View as a Generic List collection.
Action method for inserting multiple records
Inside this Action method, the JSON array of Customers records is received as Generic List Collection of Customer Entity Class objects.
First all the records of the Customers Table are deleted by using the TRUNCATE command and then a loop is executed over the Generic List Collection of Customer Entity Class objects and one by one records are inserted into database using Entity Framework.
Finally the count of the records are returned back to the jQuery AJAX function.
public class HomeController : Controller
    // GET: Home
    public ActionResult Index()
        CustomersEntities entities = new CustomersEntities();
        return View(entities.Customers);
    public JsonResult InsertCustomers(List<Customer> customers)
        using (CustomersEntities entities = new CustomersEntities())
            //Truncate Table to delete all old records.
            entities.Database.ExecuteSqlCommand("TRUNCATE TABLE [Customers]");
            //Check for NULL.
            if (customers == null)
                customers = new List<Customer>();
            //Loop and insert records.
            foreach (Customer customer in customers)
            int insertedRecords = entities.SaveChanges();
            return Json(insertedRecords);
Inside the View, in the very first line the Entity Framework Customer Model class is declared as Model for the View.
For displaying the records, an HTML Table is used. A loop will be executed over the Model which will generate the HTML Table rows with the Customer records.
The HTML Table consists of the following elements:
1. THEAD – The Header row.
2. TBODY – Left empty for dynamically adding (inserting) rows to the HTML Table.
3. TFOOT – The footer row, consisting of two TextBoxes and a Button for dynamically adding (inserting) rows to the HTML Table.
Adding a new row
When Add button in the Footer row of the HTML Table is clicked, the value of the Name and Country are fetched from their respective TextBoxes and are added as a new row to the HTML Table.
Removing a row
When the Remove button in any row of the HTML Table is clicked, it calls the Remove method which removes the row from the HTML Table.
Note: For more details on adding and remove rows from HTML Table using jQuery, please refer my article Add (Insert) / Remove (Delete) Table Rows dynamically using jQuery.
Inserting multiple rows to database using AJAX
When the Save All button is clicked, a loop is executed over all the rows of the HTML Table and a JSON array of Customer objects is generated.
The JSON array is then sent to the Controller using jQuery AJAX function and once the response is received it is displayed using JavaScript Alert Message Box.
@model IEnumerable<Insert_Multiple_Rows_EF_MVC.Customer>
    Layout = null;
<!DOCTYPE html>
    <meta name="viewport" content="width=device-width"/>
    <table id="tblCustomers" class="table" cellpadding="0" cellspacing="0">
                <th style="width:150px">Name</th>
                <th style="width:150px">Country</th>
            @foreach (Customer customer in Model)
                    <td><input type="button" value="Remove" onclick="Remove(this)"/></td>
                <td><input type="text" id="txtName"/></td>
                <td><input type="text" id="txtCountry"/></td>
                <td><input type="button" id="btnAdd" value="Add"/></td>
    <input type="button" id="btnSave" value="Save All"/>
    <script type="text/javascript" src=""></script>
    <script type="text/javascript" src=""></script>
    <script type="text/javascript">
        $("body").on("click", "#btnAdd", function () {
            //Reference the Name and Country TextBoxes.
            var txtName = $("#txtName");
            var txtCountry = $("#txtCountry");
            //Get the reference of the Table's TBODY element.
            var tBody = $("#tblCustomers > TBODY")[0];
            //Add Row.
            var row = tBody.insertRow(-1);
            //Add Name cell.
            var cell = $(row.insertCell(-1));
            //Add Country cell.
            cell = $(row.insertCell(-1));
            //Add Button cell.
            cell = $(row.insertCell(-1));
            var btnRemove = $("<input />");
            btnRemove.attr("type", "button");
            btnRemove.attr("onclick", "Remove(this);");
            //Clear the TextBoxes.
        function Remove(button) {
            //Determine the reference of the Row using the Button.
            var row = $(button).closest("TR");
            var name = $("TD", row).eq(0).html();
            if (confirm("Do you want to delete: " + name)) {
                //Get the reference of the Table.
                var table = $("#tblCustomers")[0];
                //Delete the Table row using it's Index.
        $("body").on("click", "#btnSave", function () {
            //Loop through the Table rows and build a JSON array.
            var customers = new Array();
            $("#tblCustomers TBODY TR").each(function () {
                var row = $(this);
                var customer = {};
                customer.Name = row.find("TD").eq(0).html();
                customer.Country = row.find("TD").eq(1).html();
            //Send the JSON array to Controller using AJAX.
                type: "POST",
                url: "/Home/InsertCustomers",
                data: JSON.stringify(customers),
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (r) {
                    alert(r + " record(s) inserted.");
Dynamically add rows to HTML Table in ASP.Net MVC