i am inserting row from html to database,then each row from html table inserting into database two times,
$('#btnSave').click(function () {
var smallbales = new Array();
$("#DataInsert tbody tr").each(function () {
var row = $(this);
var smallbale = {};
smallbale.Barcode_Bale = row.find("TD").eq(0).html();
smallbale.Bale_Qty = row.find("TD").eq(1).html();
smallbale.select2 = row.find("TD").eq(2).html();
smallbale.select2 = row.find("TD").eq(3).html();
smallbales.push(smallbale);
});
$.ajax({
type: "POST",
url: '/OrderPack/InsertPack',
data: JSON.stringify(smallbales),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (r) {
alert("Record(s) inserted.");
},
error: function (r) {
alert(r.responseText);
}
});
});
public ActionResult InsertPack(List<OrderVm> smallbales)
{
if (smallbales != null)
{
foreach (OrderVm smallbale in smallbales)
{
var newbigbale = new tbl_PckDetail();
newbigbale.Prdno = smallbale.Barcode_Bale;
newbigbale.QTY = smallbale.Bale_QTY;
DB.tbl_PckDetail.Add(newbigbale);
DB.SaveChanges();
}
}
return View("Index");
}
public class OrderVm
{
[Display(Name = "Select Order")]
public int? OrderNo { get; set; }
public int? SOrderNo { get; set; }
public int? Codeitem { get; set; }
[Display(Name = "Input Barcode")]
public int? Prdno { get; set; }
public int? Barcode_Bale { get; set; }
public int? orderqty { get; set; }
public int? prdqty { get; set; }
public int? packQty { get; set; }
public int? PID { get; set; }
public int? Bale_QTY { get; set; }
public int? weight { get; set; }
public Probale smallbale { get; set; }
public ItemMasterFile Itemasterfile { get; set; }
}