将Viewbag从Action Controller传递到局部视图

将Viewbag从Action Controller传递到局部视图

问题描述:

我有一个带有局部视图的mvc视图.控制器中有一个ActionResult方法,该方法将返回PartialView.因此,我需要将ViewBag数据从该ActionResult方法传递给Partial View.

I have a mvc view with a partial view.There is a ActionResult method in the controller which will return a PartialView. So, I need to pass ViewBag data from that ActionResult method to Partial View.

这是我的控制器

public class PropertyController : BaseController
{
    public ActionResult Index()
    {
        return View();
    }
    public ActionResult Step1()
    {
        ViewBag.Hello = "Hello";
        return PartialView();
    }
}

在Index.cshtml视图中

In Index.cshtml View

@Html.Partial("Step1")

Step1.cshtml部分视图

Step1.cshtml partial view

@ViewBag.Hello

但是这不起作用.那么,什么是从viewbag中获取数据的正确方法. 我认为我使用了错误的方法.请引导我.

But this is not working. So, what is the correct way to get data from viewbag. I think I'm following wrong method. Please guide me.

子动作与父动作遵循的控制器/模型/视图生命周期不同.因此,它们不共享ViewData/ViewBag."

"Child actions follow a different controller/model/view lifecycle than parent actions. As a result they do not share ViewData/ViewBag."

答案提供了另一种传递数据的方式.

The answer provides an alternate way of passing data.

执行子操作共享相同的ViewBag及其父项"行动?