Hi Ah1970,
Please take reference of this and correct your code.
Model
C#
public class CustomerModel
{
public string Name { get; set; }
public string Country { get; set; }
}
VB.Net
Public Class CustomerModel
Public Property Name As String
Public Property Country As String
End Class
Namespaces
C#
using System.Configuration;
using System.Data.SqlClient;
VB.Net
Imports System.Configuration
Imports System.Data.SqlClient
Controller
C#
public class HomeController : Controller
{
// GET: /Home/
public ActionResult Index()
{
return View();
}
public ActionResult InsertCustomers(List<CustomerModel> customers)
{
if (customers != null)
{
foreach (CustomerModel customer in customers)
{
string query = "INSERT INTO Customers VALUES(@Name, @Country)";
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(query))
{
cmd.Parameters.AddWithValue("@Name", customer.Name);
cmd.Parameters.AddWithValue("@Country", customer.Country);
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
}
return View("Index");
}
}
VB.Net
Public Class HomeController
Inherits System.Web.Mvc.Controller
' GET: /Home
Function Index() As ActionResult
Return View()
End Function
Function InsertCustomers(ByVal customers As List(Of CustomerModel)) As ActionResult
If customers IsNot Nothing Then
For Each customer As CustomerModel In customers
Dim query As String = "INSERT INTO Customers VALUES(@Name, @Country)"
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand(query)
cmd.Parameters.AddWithValue("@Name", customer.Name)
cmd.Parameters.AddWithValue("@Country", customer.Country)
cmd.Connection = con
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Using
End Using
Next
End If
Return View("Index")
End Function
End Class
View
<html>
<head>
<title>Index</title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
$('#btnInsert').click(function () {
var tbody = $('#DataInsert tbody');
var tr = $('<tr></tr>');
tr.append('<td>' + $('#txtName').val() + '</td>');
tr.append('<td>' + $('#txtCountry').val() + '</td>');
tr.append('<td><input class="del" type="button" value="Delete" /></td>')
tbody.append(tr);
});
$('#btnSave').click(function () {
var customers = new Array();
$("#DataInsert 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();
customers.push(customer);
});
$.ajax({
type: "POST",
url: '/Home/InsertCustomers',
data: JSON.stringify(customers),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (r) {
alert("Record(s) inserted.");
},
error: function (r) {
alert(r.responseText);
}
});
});
});
</script>
</head>
<body>
<table>
<tr>
<th>Name</th>
<th>Country</th>
</tr>
<tr>
<th><input type="text" id="txtName" /></th>
<th><input type="text" id="txtCountry" /></th>
</tr>
</table>
<input type="button" value="Insert" id="btnInsert" />
<br />
<br />
<table id="DataInsert">
<thead>
<tr>
<th>Name</th>
<th>Country</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
<br />
<input type="button" value="Save" id="btnSave" />
</body>
</html>