日期:2014-05-17 浏览次数:20423 次
@using (Html.BeginForm("Edit", "Admin")) { @Html.EditorForModel() <input type="submit" value="保存" /> @Html.ActionLink("返回", "Index") }
[HttpPost] public ActionResult Edit(Product product) { if (ModelState.IsValid) { repository.SaveProduct(product); TempData["message"] = string.Format("{0} 保存成功", product.Name); return RedirectToAction("Index"); } else { // there is something wrong with the data values return View(product); } }
public void SaveProduct(Product product) { if (product.ProductID == 0) { context.Products.Add(product); } else //else 我自己加的,我看的MVC资料里没有这个else语句块 { //context.Products.Attach(product); //这句更新的该怎么写呢?还是说这句压根不用写? } context.SaveChanges(); }