我星期一有一个考试,我想问你是否有人知道什么是最好的方式来处理ProductView中的编辑。 或多或少是关于ProductController中的代码。MVC 2 EF 4.0正确的方式来处理类似产品的编辑
也许有人知道比我的两次尝试更清洁的解决方案。
这是第一种方式,我没有:
我有这个在我的编辑
public ActionResult Edit(int id)
{
var productToEdit = (from p in db.Products
where p.ProductId == id
select p).First();
return View(productToEdit);
}
这在编辑一职的get:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(Product productToEdit)
{
try
{
// TODO: Add update logic here
var originalProducts = (from p in db.Products
where p.ProductId == productToEdit.ProductId
select p).First();
db.ApplyCurrentValues(originalProducts.EntityKey.EntitySetName, productToEdit);
db.SaveChanges();
return RedirectToAction("Index");
}
catch
{
return View();
}
}
这是一个干净的解决方案?
这将是第二个解决方案,我认为:
考试在MVC 2做与EF 4.0。
为什么第二?你能否更详细地解释我使用第二vs第一的好处? – bfmv991