开发者

How to pass the id of the parent to the create view of a child

开发者 https://www.devze.com 2023-01-18 02:09 出处:网络
I\'m new to MVC2 and my question should be pretty basic. At least I thought so until I could\'nt find any answer on the web, so here I am.

I'm new to MVC2 and my question should be pretty basic. At least I thought so until I could'nt find any answer on the web, so here I am.

I have a parent object Pool that can have 0 to many children Question.

In my Details view of Pool, in addition to the Pool's property, I render his childs using RenderAction on the Question action List, so far, so good.

Inside my List view of Question (which is always rendered inside the Det开发者_开发技巧ails view), I want a button to start the Create action of the Question object. My problem is, I don't know how to pass the Pool object, which is the model of my Details view, to the Create action so that I can link my Question to the right Pool.

Is there a way to access the "Master" Model inside the "included" view via RenderAction and if not, what's the best way to implement a work around.

Thanks


This is one of my favourite hidden gems in MVC.. this will give you the parent model:

<% object parentModel = ViewContext.ParentActionViewContext.ViewData.Model; %>


Clicktricity's answer helped me with similar issue trying to get access to an Html.Action's parent model. I had trouble passing the model and docOrdinal in the same call and was able to work around it.

[ChildActionOnly]
public ActionResult ActiveDocumentsDegree(string docOrdinal) 
{
    var model = (UserModel)ControllerContext.ParentActionViewContext.ViewData.Model;
    .
    .
    .
}
0

精彩评论

暂无评论...
验证码 换一张
取 消