Emmmanuel FIADUFE

Emmmanuel FIADUFE

  • 791
  • 1.1k
  • 53.7k

Controller not passing data into the tables

Aug 7 2023 4:52 PM

Hello Team,

Please I have 3 tables namely, Category table, product table and quantity table,

at the product page I select the category and then add the product price quantity and price and save it, but it doesn't get save when I try to save the data, kindly help.

public ActionResult SaveItem(ItemViewModel itemModel)
{

    if (itemModel.ItemId > 0)
    {
        tblItem objItem = objRestaurantDBEntities.tblItems.Where(a => a.ItemId == itemModel.ItemId).FirstOrDefault<tblItem>();

        objItem.CategoryId = itemModel.CategoryId;
        objItem.CategoryName = itemModel.tblCategory.CategoryName;
        objItem.ItemName = itemModel.ItemName;
        objItem.TransactionId = itemModel.QuantityId;
        objItem.Quantity = itemModel.tblQuantity.Quantity;
        objItem.ItemPrice = itemModel.ItemPrice;
        objRestaurantDBEntities.SaveChanges();
        objItem.ItemId = itemModel.ItemId;
        return Json(new { result = true, message = "Product updated successfully...." }, JsonRequestBehavior.AllowGet);
    }

    tblItem objItemSave = new tblItem();
    objItemSave.CategoryId = itemModel.CategoryId;
    objItemSave.CategoryName = itemModel.tblCategory.CategoryName;
    objItemSave.ItemName = itemModel.ItemName;
    objItemSave.TransactionId = itemModel.QuantityId;
    objItemSave.Quantity = itemModel.tblQuantity.Quantity;
    objItemSave.ItemPrice = itemModel.ItemPrice;
    objRestaurantDBEntities.tblItems.Add(objItemSave);
    objRestaurantDBEntities.SaveChanges();

    return Json(new { result = true, message = "Product save successfully...." }, JsonRequestBehavior.AllowGet);
}

 


Answers (2)