Quote:
jQuery("#tblrows tr").each(function () {
var item = {
"Product_ID": $('.Product_ID').val(),
"Product_Price": $('.Product_Price').val(),
"Product_Quantity": $('.Product_Quantity').val(),
"Unit_ID": $('.Unit_ID').val()
}
lstitems.push(item);
});
Those selectors will return the value of the
first element in the document with the specified class.
You want to return the value of the element
within the current row.
You need to pass
this
as the second parameter to the
$
function:
$("#tblrows tr").each(function() {
var item = {
"Product_ID": $('.Product_ID', this).val(),
"Product_Price": $('.Product_Price', this).val(),
"Product_Quantity": $('.Product_Quantity', this).val(),
"Unit_ID": $('.Unit_ID', this).val()
}
lstitems.push(item);
});
Assuming you have suitable navigation properties defined on your entity classes, you can also simplify your controller:
[HttpPost]
public IActionResult addorder([FromBody]OrderMasterDetails ordermasterdetail)
{
int.TryParse(ordermasterdetail.discount, out var discount);
int.TryParse(ordermasterdetail.tax, out var tax);
OrderMaster orderMaster = new OrderMaster
{
Order_Code = ordermasterdetail.Order_Code,
Hours = DateTime.Now.Hour.ToString(),
discount = discount,
DateAdded = DateTime.Now,
BranchID = int.Parse(ordermasterdetail.BranchID),
Order_Kind_ID = int.Parse(ordermasterdetail.Order_Kind_ID),
tax = tax,
};
_Context.orderMasters.Add(orderMaster);
foreach (var item in ordermasterdetail.lstProducts)
{
OrderDetails orderDetails = new OrderDetails
{
Order = orderMaster,
Product_ID = int.Parse(item.Product_ID),
Product_Price = float.Parse(item.Product_Price),
Product_Quantity = int.Parse(item.Product_Quantity),
Unit_ID = int.Parse(item.Unit_ID),
};
_Context.orderDetails.Add(orderDetails);
}
_Context.SaveChanges();
orderMaster.Order_Code = orderMaster.Order_Code + orderMaster.Order_ID.ToString();
_Context.SaveChanges();
return Ok();
}