私は本Pro ASP.NET MVC3 Framework - Freeman Sandersanを読んでいます。ここでは、Web ショップで簡単な管理を行う方法が示されています。問題は、1 つの製品の変更を保存しようとすると保存されないことです。
編集ビュー:
@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」に変更しました。保存が完了したという一時メッセージが表示されますが、製品名は「Kayak」のままです。