Hello Team,
I use below function for saving data into my Sql Database from my controller and it works fine and as am trying to use same function for update query but is not working.
public ActionResult SaveItem(ItemViewModel itemModel)
{
var result = false;
try
{
decimal costPrice = itemModel.CostPrice;
decimal sellingPrice =itemModel.ItemPrice;
decimal Profit = sellingPrice - costPrice;
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;
}
tblQuantity objQnty = new tblQuantity();
if (itemModel.QuantityId > 0)
{
objQnty = objRestaurantDBEntities.tblQuantities
.FirstOrDefault(x => x.QuantityId == itemModel.QuantityId);
}
objQnty.Quantity = itemModel.Quantity;
objQnty.QuantityId = itemModel.QuantityId;
if (itemModel.QuantityId <= 0)
{
objRestaurantDBEntities.tblQuantities.Add(objQnty);
}
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.CategoryId = itemModel.CategoryId;
objItem.Active = itemModel.Active;
if (itemModel.ItemId <= 0)
{
objRestaurantDBEntities.tblItems.Add(objItem);
}
objRestaurantDBEntities.SaveChanges();
result = true;
}
catch (Exception ex)
{
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) {
var itemModel = 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.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);
}
});
}
Emmmanuel FIADUFEPosted Jun 22, 2024, 8:23 PM
Hello Team
Please any help on this
Emmmanuel FIADUFEPosted Jun 19, 2024, 12:08 PM
Hello Mr Jayraj
This is my query for fetching or binding the data which is perfectly working
public JsonResult UpdateItem(int? ItemId, int? quantity)
{
var dataList = objRestaurantDBEntities.tblItems.Include("tblCategory").Include("tblQuantities").ToList();
var modifiedData = dataList.AsEnumerable().Where(x => x.ItemId == ItemId).Select(d => new bindItem
{
ItemId = d.ItemId,
CategoryId = d.CategoryId,
PCode = d.PCode,
CategoryName = d.tblCategory.CategoryName,
QuantityId = d.tblQuantities.FirstOrDefault().QuantityId,
Quantity = d.tblQuantities.FirstOrDefault().Quantity,
ItemName = d.ItemName,
CostPrice = d.CostPrice,
ItemPrice = d.ItemPrice,
Profit = d.Profit,
Active = d.Active
}).FirstOrDefault();
return Json(modifiedData, JsonRequestBehavior.AllowGet);
}
function getItemforUpdate($ItemId)
{
var itemModel = new Object();
$("#itemId").val($ItemId);
if ($ItemId != 0) {
jQuery.ajax({
url: "@Url.Action("UpdateItem", "Home")" + "?ItemId=" + $ItemId + "&quantity=abc",
data:({
ItemId: $ItemId
}),
type: "Get",
dataType: "json",
contentType: "application/json; charset=utf-8",
data: itemModel,
success: function (itemModel) {
if (itemModel != null) {
$("#pCode").val(itemModel.PCode);
$("#itemId").val(itemModel.ItemId)
$("#categoryId").val(itemModel.CategoryId)
$("#itemName").val(itemModel.ItemName)
$("#txtCostPrice").val( itemModel.CostPrice);
$("#quantity").val(itemModel.Quantity);
$("#itemPrice").val(itemModel.ItemPrice)
$("#status").val(itemModel.Active)
}
}
});
}
else {
$("#itemId").val("");
$("#categoryId").val("");
$("#pCode").val("");
$("#itemName").val("");
$("#txtCostPrice").val("");
$("#quantity").val("");
$("#itemPrice").val("");
$("#status").val("");
}
$("#UpdateData").show();
$("#SubmitData").hide();
$("#exampleModal").modal('show');
}
Jayraj ChhayaPosted Jun 19, 2024, 12:01 PM
The issue with the update functionality in the provided code seems to be related to how the
UpdateforProductfunction is being called and the data being passed to theSaveItemaction method in the Home controller.In the
UpdateforProductfunction, theItemIdparameter is being passed, but it is not being used to fetch the existing item from the database for updating. Instead, the function is creating a newitemModelobject and populating it with values from various HTML elements.To fix this issue, you need to modify the
UpdateforProductfunction to fetch the existing item from the database using theItemIdparameter. You can then update the fetched item with the new values from the HTML elements and send it to theSaveItemaction method for updating.