将Viewbag从Action Controller传递到局部视图 [英] Pass viewbag to partial view from action controller

查看:104
本文介绍了将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及其父项"行动?

这篇关于将Viewbag从Action Controller传递到局部视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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