Hello Team, this is my Home controller query, it always duplicate data when I try to update saved data, kindly help.
public JsonResult UpdateInvoiceSale(tblSale sale, List salesDetail, List deleted) { MasterMVCPOS.Helper.AppHelper.ReturnMessage retMessage = new AppHelper.ReturnMessage(); ASPNETMASTERPOSTEntities db = new ASPNETMASTERPOSTEntities(); retMessage.IsSuccess = true; 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.tblSales.Add(sale); retMessage.Message = "Save successfully!"; } } try { db.SaveChanges(); } catch (Exception) { retMessage.IsSuccess = false; } return Json(retMessage, JsonRequestBehavior.AllowGet); }