我想为我们的问题跟踪应用程序做一个简单的编辑表单.为简单起见,HttpGet Edit操作看起来像这样:
// Issues/Edit/12
public ActionResult Edit(int id)
{
var thisIssue = edmx.Issues.First(i => i.IssueID == id);
return View(thisIssue);
}
Run Code Online (Sandbox Code Playgroud)
然后HttpPost动作看起来像这样:
[HttpPost]
public ActionResult Edit(int id, FormCollection form)
{
// this is the dumb part where I grab the object before I update it.
// concurrency is sidestepped here.
var thisIssue = edmx.Issues.Single(c => c.IssueID == id);
TryUpdateModel(thisIssue);
if (ModelState.IsValid)
{
edmx.SaveChanges();
TempData["message"] = string.Format("Issue #{0} successfully modified.", id);
return RedirectToAction("Index");
}
return View(thisIssue);
}
Run Code Online (Sandbox Code Playgroud)
这非常有效.但是,并发检查不起作用,因为在Post中,我正在尝试更新它之前重新检索当前实体.但是,对于EF,我不知道如何使用它的好看SaveChanges()但是将我附加thisIssue到上下文中.我试着打电话 …