如何将父代的ID传递给子代的创建视图 [英] How to pass the id of the parent to the create view of a child

查看:115
本文介绍了如何将父代的ID传递给子代的创建视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是MVC2的新手,我的问题应该很简单.至少我以为是这样,直到在网上找不到任何答案为止,所以我在这里.

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.

我有一个父对象Pool,可以有0到多个子问题.

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

在"Pool"的详细信息"视图中,除了"Pool"的属性外,到目前为止,我还使用问题"操作列表上的"RenderAction"渲染了他的孩子.

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.

是否有一种方法可以通过RenderAction访问包含"视图中的主"模型,如果没有,什么是实现替代方案的最佳方法.

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.

谢谢

推荐答案

这是我在MVC中最喜欢的隐藏宝石之一.这将为您提供父模型:

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

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

这篇关于如何将父代的ID传递给子代的创建视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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