【发布时间】:2009-05-21 20:57:24
【问题描述】:
如果我的数据库中有 2 个表:Foo 和 bar。 Foo 由 FooId 标识,Bar 由 BarId 标识。一个 Bar 可以有 0 到多个 Foos 因此 Foo 有 BarId 作为外键。
我有一个代表这个的模型和一个可用于编辑 Foo 并选择(从下拉列表中)关联的 Bar 的视图。
给定控制器上的以下方法:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(int id, FormCollection formCollection)
{
Foo originalFoo = FooById(id);
if (!ModelState.IsValid)
{
return View(new VenueViewModel(originalVenue, _db.GetCounties(), _db.VenueTypeSet));
}
UpdateModel(originalFoo);
/* Instead of using UpdateModel I could just iterate through
formCollection and manually update originalFoo, it would
work but surely there is a better way? */
_db.SaveChanges();
return RedirectToAction("Index");
}
对 UpdateModel 的调用会引发 InvalidOperationException,但没有 InnerException:
The model of type 'TestApplication.Models.Foo' was not successfully updated.
我的控制器从视图中的下拉列表中更新基于实体框架的模型的正确方法是什么?
【问题讨论】:
标签: asp.net-mvc entity-framework