{"id":99652,"date":"2025-03-01T21:42:46","date_gmt":"2025-03-01T18:12:46","guid":{"rendered":"https:\/\/nabfollower.com\/blog\/controller-not-bee-able-to-pass-data-to-quantity-table-3m48\/"},"modified":"2025-03-01T21:42:46","modified_gmt":"2025-03-01T18:12:46","slug":"controller-not-bee-able-to-pass-data-to-quantity-table-3m48","status":"publish","type":"post","link":"https:\/\/nabfollower.com\/blog\/controller-not-bee-able-to-pass-data-to-quantity-table-3m48\/","title":{"rendered":"\u06a9\u0646\u062a\u0631\u0644 \u06a9\u0646\u0646\u062f\u0647 \u0632\u0646\u0628\u0648\u0631 \u0639\u0633\u0644 \u0642\u0627\u062f\u0631 \u0628\u0647 \u0627\u0646\u062a\u0642\u0627\u0644 \u062f\u0627\u062f\u0647 \u0647\u0627 \u0628\u0647 \u062c\u062f\u0648\u0644 \u06a9\u0645\u06cc\u062a \u0646\u06cc\u0633\u062a"},"content":{"rendered":"<div data-article-id=\"2305167\" id=\"article-body\">\n<p>`Public ActionResult SaveItem (AttemViewModel ArticleModel)<br \/>{<br \/>\u0646\u062a\u06cc\u062c\u0647 bool = false \u061b<\/p>\n<div class=\"highlight js-code-highlight\">\n<pre class=\"highlight plaintext\"><code>try\n{\n    \/\/ Step 1: Save Category\n    tblCategory objCategory = objRestaurantDBEntities.tblCategories\n        .SingleOrDefault(x =&gt; x.CategoryId == itemModel.CategoryId) ?? new tblCategory();\n\n    if (itemModel.CategoryId &lt;= 0)\n    {\n        objCategory.CategoryName = itemModel.CategoryName;\n        objRestaurantDBEntities.tblCategories.Add(objCategory);\n        objRestaurantDBEntities.SaveChanges(); \/\/ Save to generate CategoryId\n    }\n\n    \/\/ Ensure objCategory has an ID\n    if (objCategory.CategoryId &lt;= 0)\n    {\n        return Json(new { success = false, message = \"Failed to save category.\" });\n    }\n\n    \/\/ Step 2: Save Item\n    tblItem objItem = objRestaurantDBEntities.tblItems\n        .SingleOrDefault(x =&gt; x.ItemId == itemModel.ItemId) ?? new tblItem();\n\n    objItem.PCode = itemModel.PCode;\n    objItem.ItemName = itemModel.ItemName;\n    objItem.CostPrice = itemModel.CostPrice;\n    objItem.ItemPrice = itemModel.ItemPrice;\n    objItem.Vat = itemModel.Vat;\n    objItem.ExpiringDate = itemModel.ExpiringDate;\n    objItem.Active = itemModel.Active;\n    objItem.CategoryId = objCategory.CategoryId; \/\/ Assign correct category ID\n\n    if (itemModel.ItemId &lt;= 0) \/\/ New Item\n    {\n        objRestaurantDBEntities.tblItems.Add(objItem);\n    }\n\n    objRestaurantDBEntities.SaveChanges(); \/\/ Save Item\n\n\n    if (objItem.ItemId &lt;= 0)\n    {\n        return Json(new { success = false, message = \"Failed to save item. ItemId not generated.\" });\n    }\n\n    \/\/ Step 3: Save Quantity (Ensure itemModel.tblQuantities is not null)\n    if (itemModel.tblQuantities != null)\n    {\n        tblQuantity objQnty = objRestaurantDBEntities.tblQuantities\n            .SingleOrDefault(x =&gt; x.QuantityId == itemModel.QuantityId) ?? new tblQuantity();\n\n        objQnty.InitialQty = itemModel.tblQuantities.InitialQty;\n        objQnty.Quantity = itemModel.tblQuantities.Quantity;\n        objQnty.ItemId = objItem.ItemId;\n\n        if (itemModel.tblQuantities.QuantityId &lt;= 0) \/\/ New Quantity Entry\n        {\n            objRestaurantDBEntities.tblQuantities.Add(objQnty);\n        }\n\n        objRestaurantDBEntities.SaveChanges(); \/\/ Save Quantity Data\n    }\n\n    result = true;\n}\ncatch (Exception ex)\n{\n    \/\/ Log the error properly (Assuming you have a logging mechanism)\n    Console.WriteLine(ex.ToString());\n\n    return Json(new { success = false, message = \"An error occurred: {ex.Message}\" });\n}\n\nreturn Json(new { success = result, message = \"Item saved successfully.\" });\n<\/code><\/pre>\n<div class=\"highlight__panel js-actions-panel\">\n<div class=\"highlight__panel-action js-fullscreen-code-action\">\n    <svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"20px\" height=\"20px\" viewbox=\"0 0 24 24\" class=\"highlight-action crayons-icon highlight-action--fullscreen-on\"><title>\u062d\u0627\u0644\u062a \u062a\u0645\u0627\u0645 \u0635\u0641\u062d\u0647 \u0631\u0627 \u0648\u0627\u0631\u062f \u06a9\u0646\u06cc\u062f<\/title>\n    <path d=\"M16 3h6v6h-2V5h-4V3zM2 3h6v2H4v4H2V3zm18 16v-4h2v6h-6v-2h4zM4 19h4v2H2v-6h2v4z\"\/>\n<\/svg><\/p>\n<p>    <svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"20px\" height=\"20px\" viewbox=\"0 0 24 24\" class=\"highlight-action crayons-icon highlight-action--fullscreen-off\"><title>\u0627\u0632 \u062d\u0627\u0644\u062a \u062a\u0645\u0627\u0645 \u0635\u0641\u062d\u0647 \u062e\u0627\u0631\u062c \u0634\u0648\u06cc\u062f<\/title>\n    <path d=\"M18 7h4v2h-6V3h2v4zM8 9H2V7h4V3h2v6zm10 8v4h-2v-6h6v2h-4zM8 15v6H6v-4H2v-2h6z\"\/>\n<\/svg><\/p>\n<\/div>\n<\/div>\n<\/div>\n<p>}<br \/>`<\/p>\n<\/p><\/div>\n","protected":false},"excerpt":{"rendered":"<p>`Public ActionResult SaveItem (AttemViewModel ArticleModel){\u0646\u062a\u06cc\u062c\u0647 bool = false \u061b try { \/\/ Step 1: Save Category tblCategory objCategory = objRestaurantDBEntities.tblCategories .SingleOrDefault(x =&gt; x.CategoryId == itemModel.CategoryId) ?? new tblCategory(); if (itemModel.CategoryId &lt;= 0) { objCategory.CategoryName = itemModel.CategoryName; objRestaurantDBEntities.tblCategories.Add(objCategory); objRestaurantDBEntities.SaveChanges(); \/\/ Save to generate CategoryId } \/\/ Ensure objCategory has an ID if (objCategory.CategoryId &lt;= 0) { &hellip;<\/p>\n","protected":false},"author":2,"featured_media":99653,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"","fifu_image_alt":"","footnotes":""},"categories":[339],"tags":[],"class_list":["post-99652","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-dev"],"_links":{"self":[{"href":"https:\/\/nabfollower.com\/blog\/wp-json\/wp\/v2\/posts\/99652","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nabfollower.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nabfollower.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nabfollower.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/nabfollower.com\/blog\/wp-json\/wp\/v2\/comments?post=99652"}],"version-history":[{"count":0,"href":"https:\/\/nabfollower.com\/blog\/wp-json\/wp\/v2\/posts\/99652\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nabfollower.com\/blog\/wp-json\/wp\/v2\/media\/99653"}],"wp:attachment":[{"href":"https:\/\/nabfollower.com\/blog\/wp-json\/wp\/v2\/media?parent=99652"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nabfollower.com\/blog\/wp-json\/wp\/v2\/categories?post=99652"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nabfollower.com\/blog\/wp-json\/wp\/v2\/tags?post=99652"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}