Hello Team,
I have three tables such as tblItem, tblQuantity, and tblCategory, I select product category then try to save data into product table tables and quantity tables but data is not saving , can anyone help please
public ActionResult SaveItem(ItemViewModel itemModel) { var result = false; try { tblCategory objCategory = new tblCategory(); if (itemModel.CategoryId > 0) { objCategory = objRestaurantDBEntities.tblCategories.Where(x => x.CategoryId == itemModel.CategoryId).FirstOrDefault<tblCategory>(); } else { objCategory.CategoryName = itemModel.CategoryName; if (itemModel.CategoryId <= 0) objRestaurantDBEntities.tblCategories.Add(objCategory); objRestaurantDBEntities.SaveChanges(); objCategory.CategoryId = itemModel.CategoryId; } tblQuantity objQnty = new tblQuantity(); if (itemModel.QuantityId > 0) { objQnty = objRestaurantDBEntities.tblQuantities.Where(x => x.QuantityId == itemModel.QuantityId).FirstOrDefault<tblQuantity>(); } objQnty.Quantity = itemModel.Quantity; objQnty.TypeId = objQnty.TypeId; if (itemModel.QuantityId <= 0) { objRestaurantDBEntities.tblQuantities.Add(objQnty); } tblItem objItem = new tblItem(); if (itemModel.ItemId > 0) { objItem = objRestaurantDBEntities.tblItems.Where(x => x.ItemId == itemModel.ItemId).FirstOrDefault<tblItem>(); } objItem.PCode = itemModel.PCode; objItem.ItemName = itemModel.ItemName; objItem.ItemPrice = itemModel.ItemPrice; objItem.CategoryId = itemModel.CategoryId; objItem.Active = itemModel.Active; if (itemModel.ItemId <= 0) { objRestaurantDBEntities.tblItems.Add(objItem); } objRestaurantDBEntities.SaveChanges(); result = true; } catch (Exception ex) { throw ex; } return Json(result, JsonRequestBehavior.AllowGet); } function SaveProduct() { debugger; var itemModel = new Object(); itemModel.PCode = $("#pCode").val(); itemModel.QuantityId = $("#qtyId").val(); itemModel.Quantity = $("#quantity").val(); itemModel.ItemId = $("#itemId").val(); itemModel.CategoryId = $("#categoryId").val(); itemModel.ItemName = $("#itemName").val(); itemModel.ItemPrice = $("#itemPrice").val(); itemModel.Active = $("#status").val(); var data = JSON.stringify({ itemModel: itemModel }); $.ajax({ contentType: 'application/json; charset=utf-8', dataType: 'JSON', type: 'POST', url: "/Home/SaveItem", data:data, success: function (response) { if (response.message) { alert(response.message = "Success") DataTable.ajax.reload(); } }, error: function (msg) { alert(msg.responsetext); } }); } public class ItemViewModel { public int ItemId { get; set; } public string PCode { get; set; } public string ItemName { get; set; } public decimal ItemPrice { get; set; } public string Status { get; set; } public int CategoryId { get; set; } public string CategoryName { get; set; } public int Quantity { get; set; } public int QuantityId { get; set; } public int Active { get; set; } }