public JsonResult UpdateInvoiceSale(tblSale sale, List salesDetail, List deleted)
{
MasterMVCPOS.Helper.AppHelper.ReturnMessage retMessage = new AppHelper.ReturnMessage();
ASPNETMASTERPOSTEntities db = new ASPNETMASTERPOSTEntities();
retMessage.IsSuccess = true;
try
{
if (deleted != null)
{
foreach (var item in deleted)
{
var data = db.tblSalesDetails.Where(x => x.SalesDetailId == item).FirstOrDefault();
db.tblSalesDetails.Remove(data);
}
}
foreach (var item in salesDetail)
{
if (item.SalesDetailId > 0)
{
db.Entry(item).State = EntityState.Modified;
retMessage.Message = "Update successfully!";
}
else
{
//sale.tblSalesDetails.Add(new tblSalesDetail { ProductId = item.ProductId, UnitPrice = item.UnitPrice, Quantity = item.Quantity, LineTotal = item.LineTotal });
var prd = db.tblProductStocks.Where(x => x.ProductId == item.ProductId && x.Quantity > 0).FirstOrDefault();
prd.Quantity = prd.Quantity - item.Quantity;
db.Entry(prd).State = EntityState.Modified;
db.tblSalesDetails.Add(tblSaleDetail);
retMessage.Message = "Save successfully!";
}
}
//tblsale = sale;
db.Entry(tblsale).State = EntityState.Modified;
db.SaveChanges();
}
catch (Exception ex)
{
retMessage.IsSuccess = false;
}
return Json(retMessage, JsonRequestBehavior.AllowGet);
}
Loading
Vishal JoshiPosted Dec 12, 2023, 7:27 AM
Hello,
You need to fetch data from database and then assign updated values and save data. please check the below code.
Thanks
Vishal Joshi
Emmmanuel FIADUFEPosted Dec 12, 2023, 11:05 AM
Thank you team, it is working now perfectly
Jayraj ChhayaPosted Dec 11, 2023, 2:33 PM
Problem
I have identified a bug that needs to be addressed. The bug is related to the update of the sales details. When an existing sales detail is being updated, the code correctly sets the state of the
itementity toEntityState.Modified. However, the code is missing the necessary line to update the corresponding sales details in the database.Cause
The cause of the bug is the missing line of code that updates the sales detail entity in the database when it is being modified.
Solution
To fix the bug, we need to add the line of code that updates the sales detail entity in the database when it is being modified. We can achieve this by adding the following line of code after setting the state of the
itementity toEntityState.Modified:The updated code should look like this:
With this fix, the sales detail entity will be correctly updated in the database when it is being modified.