Dear Sir
I am developing application in asp.net mvc core.I want to save multiple records by single save button
I used "https://www.aspsnippets.com/Articles/Insert-Save-Multiple-rows-records-to-database-using-Entity-Framework-in-ASPNet-MVC.aspx"
In the code everything is working fine but in customers.push(customer), customers as parameter do not have values in server side.It is showing count 0 in " InsertCustomers" function in customercontroller
i have given code below.
Please help me Sir
Below file is Views/Customers/Index.cshtml
<script type="text/javascript">
var customers = new Array();
$("body").on("click", "#btnAdd", function ()
{
var Name = $("#txtName").val();
var Country = $("#txtCountry").val();
var tBody = $("#tblCustomers > TBODY")[0];
var row = tBody.insertRow(-1);
//Add Name cell.
var cell = $(row.insertCell(-1));
cell.html($('#txtName').val());
//Add Country cell.
cell = $(row.insertCell(-1));
cell.html($('#txtCountry').val());
//Add Button cell.
cell = $(row.insertCell(-1));
var btnRemove = $("<input />");
btnRemove.attr("type", "button");
btnRemove.attr("onclick", "Remove(this);");
btnRemove.val("Remove");
cell.append(btnRemove);
//Clear the
$("#txtName").val("")
$("#txtCountry").val("")
});
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.
table.deleteRow(row[0].rowIndex);
}
};
$("body").on("click", "#btnSave", function () {
//Loop through the Table rows and build a JSON array.
alert();
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();
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(r + " record(s) inserted.");
}
})
});
</script>
Below file is in customercontroller , function name is Insertcustomers
[Route("Customers/InsertCustomers")]
public JsonResult InsertCustomers(List<Customer> customers)// not getting values in customers
{
using (DBEmployeeContext entities = new DBEmployeeContext())
{
//Truncate Table to delete all old records.
// int a= await Context.Database.ExecuteSqlCommandAsync("DELETE FROM [Blogs]");
//Check for NULL.
if (customers == null)
{
customers = new List<Customer>();
}
//Loop and insert records.
foreach (Customer customer in customers)
{
entities.Customer.Add(customer);
}
int insertedRecords = entities.SaveChanges();
return Json(insertedRecords);
}
}
Model is
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace WebApplication29.Models
{
public partial class Customer
{
[Key]
public int CustomerId { get; set; }
public string Name { get; set; }
public string Country { get; set; }
}
}
Thanks
Chandran