Hello Team,
Am having some chanllegings here, I am trying to pass data from my controller to the database through my modelView,
But keep getting this error, object undefined.
public JsonResult SaveProduct(ProductViewModel productModel)
{
ASPNETMASTERPOSTEntities db = new ASPNETMASTERPOSTEntities();
var result = false;
try
{
tblCategory category = new tblCategory();
if (productModel.tblCategory.CategoryId > 0)
{
category = db.tblCategories.Where(x => x.CategoryId == productModel.CategoryId).FirstOrDefault();
}
else
{
category.CategoryName = productModel.tblCategory.CategoryName;
if (productModel.tblCategory.CategoryId <= 0)
db.tblCategories.Add(category);
db.SaveChanges();
category.CategoryId = productModel.tblCategory.CategoryId;
}
tblProduct product = new tblProduct();
if (productModel.ProductId > 0)
{
product = db.tblProducts.Where(a => a.ProductId == productModel.ProductId).FirstOrDefault();
}
product.ProductName = productModel.ProductName;
product.Status = productModel.Status;
product.CategoryId = productModel.tblCategory.CategoryId;
if (productModel.ProductId <= 0)
{
db.tblProducts.Add(product);
}
db.SaveChanges();
result = true;
}
catch (Exception ex)
{
throw ex;
}
return Json(result, JsonRequestBehavior.AllowGet);
}
Vishal JoshiPosted Jul 14, 2023, 9:00 AM
Hello,
Please check the API call from javascript sending model data in the correct format. it should be the same as model productModel.
Also, need to change in API code as below.
Thanks
Vishal Joshi
Emmmanuel FIADUFEPosted Jul 14, 2023, 7:33 PM
Thank you team,
Am so much greatful
Amit MohantyPosted Jul 14, 2023, 11:20 AM