MVC3 - 如何将新创建的子对象链接到它的父? [英] MVC3 - how to link a newly created child object to its parent?

查看:110
本文介绍了MVC3 - 如何将新创建的子对象链接到它的父?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在MVC一个完整的新手,而且似乎无法让我的头围绕一个很基本的概念。

I am a complete novice at MVC, and can't seem to get my head around a very basic concept.

我有一个父对象,它包含子对象的集合。我想创建一个新的子对象,并将其链接到父对象,通过EF4坚持在数据库

I have a parent object, that contains a collection of child objects. I want to create a new child object, and link it to the parent object, persisted in the database via EF4


    public class Parent
    {
        public int Id { get; set; }
        public string Name { get; set; }

        public virtual List<Child> Children { get; set; }
    }

到目前为止,会发生什么情况,我非常基本的应用程序是这样的。我的用户去显示一个父对象,它包括当前孩子的列表的细节的页面。此外,还有页面上的链接添加一个新的孩子。这链接指向创建在子控制器动作,传递父ID,这又显示视图中创建一个新的子。而这正是我卡住了。我不知道如何坚持提供的父ID,这样,当我点击保存,我可以检索父对象和我的新的子对象添加到其收藏。

So far, what happens in my very basic application is this. My user goes to a page displaying the details of a parent object, which includes a list of the current children. There is also a link on that page to add a new child. That link points to a Create action on the child Controller, passing the parent Id, which in turn displays a view to create a new child. And this is where I've got stuck. I don't know how to persist the supplied parent Id so that when I click Save, I can retrieve that parent object and add my new child object to its collection.

我可能接近这完全走错了路,但我似乎无法找到如何新的子项添加到父,这是奇怪考虑它是一个情景是多么常见的基本教程。

I'm probably approaching this in completely the wrong way, but I can't seem to find any basic tutorials on how to add new child items to a parent, which is odd considering how common a scenario it is.

任何帮助真的AP preciated!

Any help is really appreciated!

非常感谢
格里

Many thanks Gerry

[更新1]

我有两个拥有CreateChild行动,最初由MVC生成,然后通过自己修改。我可以看着他们,他们没有做什么,我需要看到的,但我不敢肯定他们是如何需要改变。具体来说,我存储ViewBag内,但调用创建行动之间的父ID,它丢失,所以无法使用时,第二个创建被称为新的子项持久化到数据库中。

I have two CreateChild actions, initially generated by MVC and then modified by myself. I can see just by looking at them that they don't do what I need, but I'm not at all sure how they need to change. Specifically, I store the parent ID within the ViewBag but between the calls to the Create actions, it gets lost, and so is not available when the second Create is called to persist the new child item to the database.

public ActionResult Create(int parentId)
{
    Parent parent = db.Parents.Find(parentId);
    ViewBag.ParentId = parent.Id;
    return View();
}

[HttpPost]
public ActionResult Create(Child child)
{
    if (ModelState.IsValid)
    {
        Parent parent = db.Parents.Find(ViewBag.ParentId);
        parent.Children.Add(child);
        db.Children.Add(child);
        db.SaveChanges();
        return RedirectToAction("Index");
    }
    return View(child);
}

谢谢
格里

推荐答案

当您通过的ParentId到add孩子的行动,它看起来像您正在使用的路由参数做这件事。

When you pass the ParentId to the add child action, it looks like you are doing it with a route parameter.

相反,其存储在ViewBag的,把它写在你的孩子形式的隐藏字段。然后,当有人提交表单,该的ParentId将提交您的HttpPost操作方法。

Instead of storing it in the ViewBag, write it as a hidden field in your child form. Then, when someone submits the form, the ParentId will be submitted to your HttpPost action method.

您可以通过的ParentId你的孩子视图模型的属性做到这一点。

You can do this by making ParentId a property on your Child viewmodel.

public class Child
{
    public int ParentId { get; set; }
}

public ActionResult Create(int parentId)
{
    Parent parent = db.Parents.Find(parentId);
    var model = new Child { ParentId = parent.Id };
    return View(model);
}

在你看来,使其像这样:

In your view, render it like this:

@Html.HiddenFor(m => m.ParentId)

然后,在你的HttpPost,孩子已经包含的ParentId - 默认模型绑定将从您的表单上的隐藏字段得到它。

Then, during your HttpPost, Child will already contain ParentId -- the default model binder will get it from the hidden field on your form.

[HttpPost]
public ActionResult Create(Child child)
{
    if (ModelState.IsValid)
    {
        Parent parent = db.Parents.Find(child.ParentId);
        parent.Children.Add(child);
        db.Children.Add(child); // don't think you need this line
        db.SaveChanges();
        return RedirectToAction("Index");
    }
    return View(child);
}

公布答案后更新

看你HttpPost code,也可能是不正确的子添加到数据库的两倍。如果您使用的是EF 4.1或4.2,我是pretty确定这是不正确的,但我不知道previous EF版本。增加孩子的parent.Children收集应该是足够 - 我不认为你也应该把它添加到db.Children集。

Looking at your HttpPost code, it may be incorrect to add the child to the db twice. If you are using EF 4.1 or 4.2, I am pretty sure this is incorrect, but I'm not sure about previous EF versions. Adding the child to the parent.Children collection should be enough -- I don't think you should also add it to the db.Children set.

这篇关于MVC3 - 如何将新创建的子对象链接到它的父?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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