MVC3 - how to link a newly created child object to its parent?

☆樱花仙子☆ 提交于 2019-12-07 13:40:00

问题


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

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

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.

Any help is really appreciated!

Many thanks Gerry

[Update 1]

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

Thanks again Gerry


回答1:


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

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.

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)

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

Update after posting answer

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.




回答2:


ViewBag is not ViewState (ASP.NET MVC doesn't have any built-in equivalent to ASP.NET WebForms ViewState) - it will not keep ParentId between calls. It will just allow you passing ParentId to view (in your first action) so you can for example store it in hidden field.



来源:https://stackoverflow.com/questions/8573197/mvc3-how-to-link-a-newly-created-child-object-to-its-parent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!