views:

40

answers:

2

Hi,

assuming I have a Child and Parent object each of which has a repository and a controller.

Update: A Parent has many Child objects.

the parent's controller for creating would look something like. the post to create will be made using the auto-generated form.

    public ActionResult Create()
    {

        return View();
    }

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Create(Parent parent)
    {
        var parentRepo = new ParentRepository();

        parentRepo.Save(parent);

        return View();
    }

how would I go creating a child after I have created the parent?

1) one solution would be using a url like child/create/{idParent}

2) or even embedding the parent id in the child creating form so it is posted with the rest of the child data.

Both seem not in the spirit of MVC.

A: 

If this realy needs to be a 2 step process then I don't think the url you have is wrong. It would allow the user to refresh his browser by hitting F5 and he would still be able to enter data after refreshing. It would also be easy to display errormessages for the child object.

But I am not shure if this 2 step process is realy necessary. If you have the chance to build the complete object on 1 action it would be easier. But I am just guessing. Maybe its a design decission.

Malcolm Frexner
it needs to be a two step process as the parent has more than one children. i will update question to reflect that.
Circadian
+1  A: 

I would go with alternative 1)

In your Post version of Create parent, you can return a RedirectAction to creating a child (and add the idParent in your routeValues). If you don't like the url Child/Create/{idParent}, you could have something like Parent/{idParent}/CreateChild, depending on how it matches your controller setup, and what the main entity in your context is (Child in the first url, Parent in the second url).

Terje
of the two i have seen around the web (1) was the "best" solution. still it seemed odd. I went with it since I must finish the project.
Circadian