Hello Team, I use the below method/function to save data and as well to update that, saving data work fine but whem I tried updating the data I get duplication of data, and this happen in the tblQuantity. I have the following table tblCategory, tblProduct, and tblQuantity. can anyone help me out.
public ActionResult SaveItem(ItemViewModel itemModel) { var result = false; try { decimal vat = itemModel.Vat; decimal costPrice = itemModel.CostPrice; decimal sellingPrice =itemModel.ItemPrice; decimal Profit = sellingPrice - costPrice; decimal Vatpercent = (vat / 100) * sellingPrice; tblCategory objCategory = new tblCategory(); if (itemModel.CategoryId > 0) { objCategory = objRestaurantDBEntities.tblCategories.FirstOrDefault(x => x.CategoryId == itemModel.CategoryId); } else { objCategory.CategoryName = itemModel.CategoryName; objRestaurantDBEntities.tblCategories.Add(objCategory); objRestaurantDBEntities.SaveChanges();
// Assign the generated CategoryId after save itemModel.CategoryId = objCategory.CategoryId; } tblItem objItem = new tblItem(); if (itemModel.ItemId > 0) { objItem = objRestaurantDBEntities.tblItems.FirstOrDefault(x => x.ItemId == itemModel.ItemId); } objItem.PCode = itemModel.PCode; objItem.ItemName = itemModel.ItemName; objItem.CostPrice = itemModel.CostPrice; objItem.ItemPrice = itemModel.ItemPrice; objItem.Profit = Profit; objItem.Vat = Vatpercent; objItem.CategoryId = itemModel.CategoryId; objItem.Active = itemModel.Active; if (itemModel.ItemId <= 0) { objRestaurantDBEntities.tblItems.Add(objItem); } tblQuantity objQnty = new tblQuantity(); if (itemModel.QuantityId > 0) { objQnty = objRestaurantDBEntities.tblQuantities.FirstOrDefault(x => x.QuantityId == itemModel.QuantityId); }
objQnty.Quantity = itemModel.Quantity; objQnty.ItemId = itemModel.ItemId; if (itemModel.QuantityId <= 0) { objRestaurantDBEntities.tblQuantities.Add(objQnty); //objRestaurantDBEntities.SaveChanges(); //itemModel.QuantityId = objQnty.QuantityId; }
objRestaurantDBEntities.SaveChanges(); result = true; } catch (Exception ex) { // Log the exception or handle it appropriately // Logging ex: Logger.Log(ex); // Return a meaningful error message return Json(new { success = false, message = "An error occurred while saving the item." }); }
return Json(new { success = result, message = "Item saved successfully." }); }
function UpdateforProduct(ItemId) { debugger; var itemModel = new Object(); var objQnty = new Object(); itemModel.PCode = $("#pCode").val(); itemModel.CategoryId = $("#categoryId").val(); itemModel.QuantityId = $("#qtyId").val(); itemModel.Quantity = $("#quantity").val(); itemModel.ItemId = $("#itemId").val(); itemModel.ItemName = $("#itemName").val(); itemModel.CostPrice = $("#txtCostPrice").val(); itemModel.ItemPrice = $("#itemPrice").val(); itemModel.Vat = $("#vat").val(); itemModel.Active = $("#status").val(); $.ajax({ contentType: 'application/json; charset=utf-8', dataType: 'JSON', type: 'POST', url: '@Url.Action("SaveItem", "Home")', data: JSON.stringify({ itemModel: itemModel }), success: function (response) { if (response.message) { //alert(response.message = "Success") toastr.success(response.message = "Product successfully updated!"); DataTable.ajax.reload();
$("#exampleModal").modal('hide'); } }, error: function (msg) { toastr.danger(msg.responseText); } });
}