0
我正在看書臨ASP.NET MVC3框架 - Freeman Sandersan它顯示瞭如何使簡單的管理即時通訊網店。 問題是,當我嘗試保存一個它不保存的產品的更改時。MVC EditorForModel Freeman
編輯觀點:
@using (Html.BeginForm()) {
@Html.EditorForModel()
<input type="submit" value="Save" />}
編輯方法:
[HttpPost]
public ActionResult Edit(Product product)
{
if (ModelState.IsValid)
{
repository.SaveProduct(product);
TempData["message"] = string.Format("{0} has been saved", product.Name);
return RedirectToAction("Index");
}
// there is something wrong with the data values
return View(product);
}
保存產品的方法:
private EFDbContext context=new EFDbContext();
public void SaveProduct(Product product)
{
if (product.ProductID == 0)
{
context.Products.Add(product);
}
context.SaveChanges();
}
編輯的結果:我更改產品的名稱爲 「Kayakkkkkkkkkk」 。 Temp消息表示已完成保存,但產品名稱仍爲「皮划艇」。