public void SaveProduct(Product product)
{
if (product.ProductID == 0)
{
context.Products.Add(product);
}
//Oops~~~
context.SaveChanges();
}
[HttpPost]
public ActionResult Edit(Product product)
{
if (ModelState.IsValid)
{
repository.SaveProduct(product);
//I can see this msg int the view page. but database never changed.!!
TempData["message"] = string.Format("{0} has been saved", product.Name);
return RedirectToAction("Index");
}
else
{
// there is something wrong with the data values
return View(product);
}
}
我被这个问题困住了,不知道如何将数据存储到数据库中。当我尝试保存对现有产品的更改时会出现问题。谁能告诉我为什么调用了 saveChanges() 方法并且数据从未保存到数据库中?谢谢