MVC2 RTM - 使用实体框架模型绑定复杂对象
我是 MVC 新手,并且真的很挣扎于我认为这应该是一个非常常见的场景。我正在使用 MVC2 RTM 和模型对象的实体框架。
我正在工作:
包含子对象集合的父对象的编辑视图。该表单显示父对象的所有可编辑字段,并迭代并显示所有关联子对象的所有可编辑字段(在同一视图中)。我能够成功处理控制器中的编辑操作,但当我尝试将表单集合中的值绑定到 EF 模型对象时遇到问题。
问题:
在我的控制器函数中,当我调用 TryUpdateModel 并传递父对象时,出现以下错误:
“EntityCollection 已被初始化。仅应在对象图反序列化期间调用 InitializeRelatedCollection 方法来初始化新的 EntityCollection ”。
我看过很多其他人的帖子,都遇到过类似的问题,但还没有找到解决方案。如果不构建自定义模型绑定程序,这是否不可能?如果有人有一个有效的例子,我将不胜感激。由于某种原因,我能够迭代子集合并在子对象上成功执行 TryUpdateModel,但是当我在父对象上调用它时,会引发上述错误。理想情况下,我想为父级调用一次,并从表单更新整个对象树。
这是控制器代码:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(int id, FormCollection formValues)
{
EFEntities ef = new EFEntities();
ParentObject parent = ef.ParentObjects.SingleOrDefault(p => p.ID == id);
if (ModelState.IsValid)
{
int i = 0;
foreach (child in parent.ChildObjects)
{
//this works fine
TryUpdateModel(child, "ChildObjects[" + i + "]");
i++;
}
//this blows up
if (TryUpdateModel(parent))
{
ef.SaveChanges();
return RedirectToAction("Details", new { id = parent.ID });
}
}
return View(parent);
}
I am new to MVC, and am really struggling with what I seems like it should be a very common scenario. I'm using MVC2 RTM, and the Entity Framework for my model objects.
What I have working:
An edit view for a parent object that contains a collection of child objects. The form displays all the editable fields for the parent, and iterates through and displays all editable fields for all the associated child objects (in the same view). I am able to successfully handle the edit action in my controller, but run into issues when I try to bind values in the form collection to the EF model objects.
The problem:
In my controller function, when I call TryUpdateModel and pass the parent object, I get the following error:
"The EntityCollection has already been initialized. The InitializeRelatedCollection method should only be called to initialize a new EntityCollection during deserialization of an object graph."
I have seen a lot of other posts from people struggling with similar issues, but have not found a solution. Is this not possible without building a custom model binder? If anyone has a working example, I would greatly appreciate it. For some reason, I am able to iterate through the child collection and successfully execute TryUpdateModel on the child objects, but when I call it on the parent, the error above is thrown. Ideally I'd like to call it once for the parent, and have the whole object tree update from the form.
Here's the controller code:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(int id, FormCollection formValues)
{
EFEntities ef = new EFEntities();
ParentObject parent = ef.ParentObjects.SingleOrDefault(p => p.ID == id);
if (ModelState.IsValid)
{
int i = 0;
foreach (child in parent.ChildObjects)
{
//this works fine
TryUpdateModel(child, "ChildObjects[" + i + "]");
i++;
}
//this blows up
if (TryUpdateModel(parent))
{
ef.SaveChanges();
return RedirectToAction("Details", new { id = parent.ID });
}
}
return View(parent);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
感谢您提出这个问题,尽管没有得到回答,但它给了我答案。我能找到的最好的事情就是(使用你的例子):
Thanks for this question, even though it wasn't answered it gave me my answer. The best thing I can find to do is this (using your example):
最终我找到了一个更优雅的解决方案,但从未回来发布它。就是这样 - 抱歉耽搁了:
我正在从现实生活中的应用程序转换此代码,所以我对任何拼写错误表示歉意。
Ultimately I found a more elegant solution, but never came back to post it. Here it is - sorry for the delay:
I'm converting this code from a real life app, so my apologies for any typos.