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

痴心易碎 提交于 2019-12-25 01:32:06

问题


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 Details 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


回答1:


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

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



回答2:


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


来源:https://stackoverflow.com/questions/3834020/how-to-pass-the-id-of-the-parent-to-the-create-view-of-a-child

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