MVC2 RTM - 模型绑定使用实体框架复杂对象 [英] MVC2 RTM - model binding complex objects using Entity Framework

查看:230
本文介绍了MVC2 RTM - 模型绑定使用实体框架复杂对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的MVC,和我真的是我喜欢它似乎应该是一个很常见的场景挣扎。我使用MVC2 RTM和实体框架为我的模型对象。

我有什么工作:

有包含子对象的集合父对象的编​​辑视图。该表格​​显示了通过父所有的可编辑的字段,并循环并显示所有关联的子对象的所有可编辑字段(在同一个视图)。我能够成功地处理在我的控制器编辑动作,但遇到问题时,我尝试的形式集合中值绑定到EF模型对象。

问题:

在我的控制器功能,当我打电话TryUpdateModel并传递父对象,我得到以下错误:

的EntityCollection已经初始化该InitializeRelatedCollection方法只应调用对象图的反序列化过程中初始化一个新的EntityCollection。

我已经看到了人们类似问题挣扎了很多其他职位,但还没有找到一个解决方案。这是不可能没有建立一个自定义的模型绑定?如果任何人有一个工作的例子,我将不胜AP preciate它。出于某种原因,我能够通过子集合进行迭代,并成功地在子对象执行TryUpdateModel,但是当我把它称为父,上述错误被抛出。理想情况下,我想一次把它称为父,并有从形成整个对象树更新。

下面是控制器code:

 的[AcceptVerbs(HttpVerbs.Post)
公众的ActionResult编辑(INT ID,的FormCollection formValues​​)
        {
            EFEntities EF =新EFEntities();
            ParentObject父= ef.ParentObjects.SingleOrDefault(p值=> p.ID == ID);            如果(ModelState.IsValid)
            {
                INT I = 0;
                的foreach(在parent.ChildObjects子)
                {
                    //这工作正常
                    TryUpdateModel(孩子,ChildObjects [+ I +]);
                    我++;
                }                //这个炸毁
                如果(TryUpdateModel(父))
                {
                    ef.SaveChanges();
                    返回RedirectToAction(详细信息,新{ID = parent.ID});
                }
            }
            返回查看(父);
        }


解决方案

最后,我找到了一个更好的解决方案,却再也没有回来将它张贴。这里是 - 耽搁惋惜:

 如果(ModelState.IsValid)
        {
            如果(TryUpdateModel(父母,新的[]为prop1,prop2,prop3}))//仅指定父属性包括
            {
                如果(TryUpdateModel(parent.ChildObjectsChildObjects))
                {
                    _ef.SaveChanges();
                    返回RedirectToAction(详细信息,新{ID = parent.ID})}
            }
        }
        返回查看(父);

我是从一个现实生活中的应用程序转换这一code,所以我的道歉任何错别字。

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);
        }

解决方案

Ultimately I found a more elegant solution, but never came back to post it. Here it is - sorry for the delay:

if (ModelState.IsValid)
        {
            if (TryUpdateModel(parent, new[] "prop1", "prop2", "prop3" })) //specify parent-only properties to include
            {
                if (TryUpdateModel(parent.ChildObjects, "ChildObjects"))
                {
                    _ef.SaveChanges();
                    return RedirectToAction("Details", new { id = parent.ID })                    }
            }
        }
        return View(parent);

I'm converting this code from a real life app, so my apologies for any typos.

这篇关于MVC2 RTM - 模型绑定使用实体框架复杂对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆