本文介绍了难道一个子操作共享相同的ViewBag以其"父母和QUOT;行动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很困惑这个:
我有一个动作,说的父母,并在相应的视图文件,我已经叫孩子的动作,说孩子,无论父母与子女的行动是在同一个控制器。

I am confused with this:I have an action ,say Parent ,and in the corresponding view file ,I have called a child action ,say Child ,both Parent and Child actions are in the same controller.

和我需要的儿童动作和家长行动分享在ViewBag.Now一些数据,我应该做的这是我的问题:

and I need the Child action and the Parent action to share some data in the ViewBag.Now ,what I should do ?Here is my question:

当我打电话在父母的视图文件儿童的行动,我通过viewbag它是这样的:
@ Html.Action(ViewBag)。
在我的孩子的动作,我这样做:

when I call the Child action in parent's view file ,I pass the viewbag to it like this:@Html.Action(ViewBag).in my child action ,I do this:

public PartialViewResult Child(Object ViewBag)
{
  //using the data in ViewBag
}

这是正确的方式?是否viewbag对象按引用传递或者它是一个不同的对象则原来viewbag(需要更多的内存)?

Is this the right way ? Does the viewbag object passed by reference or it is a different object then the original viewbag(more memory needed)?

或该​​儿童的行动默认情况下其调用父行动分享viewbag?

Or if the Child action is sharing the viewbag with its calling parent Action by default?

,我知道,我不能做这样的事情:@ Html.Action(ViewBag)

From Darin Dimitrov's answer ,I knew that I can't do something like this:@Html.Action(ViewBag)

但我真的需要通过子操作穆蒂参数,我该怎么办?

But I really need to pass the child action muti-parameters,what can I do ?

推荐答案

儿童行动按照不同的控制器/模型/视图比父母的行为的生命周期。因此,他们不同意的ViewData / ViewBag。如果你想传递参数给从父项的子行动,你可以这样做:

Child actions follow a different controller/model/view lifecycle than parent actions. As a result they do not share ViewData/ViewBag. If you want to pass parameters to a child action from the parent you could do this:

@Html.Action("Child", new { message = ViewBag.Message })

和在孩子的行动:

public ActionResult Child(string message)
{
    ...
}

这篇关于难道一个子操作共享相同的ViewBag以其"父母和QUOT;行动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 03:51