简体   繁体   English

将viewbag从动作控制器传递到局部视图

[英]Pass viewbag to partial view from action controller

I have a mvc view with a partial view.There is a ActionResult method in the controller which will return a PartialView. 我有一个部分视图的mvc视图。控制器中有一个ActionResult方法,它将返回一个PartialView。 So, I need to pass ViewBag data from that ActionResult method to Partial View. 所以,我需要将ViewRag数据从ActionResult方法传递给Partial View。

This is my Controller 这是我的控制器

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

In Index.cshtml View 在Index.cshtml视图中

@Html.Partial("Step1")

Step1.cshtml partial view Step1.cshtml局部视图

@ViewBag.Hello

But this is not working. 但这不起作用。 So, what is the correct way to get data from viewbag. 那么,从viewbag获取数据的正确方法是什么。 I think I'm following wrong method. 我想我正在遵循错误的方法。 Please guide me. 请指导我。

You can use it as mentioned below : 你可以按照下面的说法使用它:

In your View : 在您的视图中:

@Html.Partial("[ViewName]", (string)ViewBag.Message)

And Your partial View : 而你的部分观点:

@model String

<b>@Model</b>

As Shown Above ViewBag.Message will be passed to the partial view. 如上所示,ViewBag.Message将传递给局部视图。 and in your partial view you can use it as a @Model . 在局部视图中,您可以将其用作@Model

Note : here type of ViewBag.Message is string . 注意:这里ViewBag.Message的类型是字符串 You can pass any type. 你可以传递任何类型。

If you don't have to use ViewBag, you can use TempData. 如果您不必使用ViewBag,则可以使用TempData。 TempData is shared for the whole execution chain. TempData是整个执行链共享的。

public class PropertyController : BaseController
{
    public ActionResult Index()
    {
        return View();
    }
    public ActionResult Step1()
    {
        TempData["Hello"] = "Hello";
        return PartialView();
    }
}

In Index.cshtml View 在Index.cshtml视图中

@Html.Partial("Step1")

Step1.cshtml partial view Step1.cshtml局部视图

@TempData["Hello"]

"Child actions follow a different controller/model/view lifecycle than parent actions. As a result they do not share ViewData/ViewBag." “子操作遵循与父操作不同的控制器/模型/视图生命周期。因此,它们不共享ViewData / ViewBag。”

The answer provides an alternate way of passing data. 答案提供了另一种传递数据的方法。

Does a child action share the same ViewBag with its "parents" action? 子动作是否与其“父母”动作共享相同的ViewBag?

Old question but if anyone here to find solution for this question.. 老问题,但如果有人在这里找到这个问题的解决方案..

You can pass viewbag value to partial with viewdatadictionary. 您可以使用viewdatadictionary将viewbag值传递给partial。

In your view: 在你看来:

@Html.Partial("_Partial", "", new ViewDataDictionary { { "permalink", ViewBag.Permalink } })

and in partial view use it like this: 在部分视图中使用它像这样:

ViewData["permalink"]

You can try this in order to pass ViewBag to partial view from action: 您可以尝试此操作,以便将ViewBag传递给操作中的部分视图:

Your controller: 你的控制器:

public class PropertyController : Controller
{
    public ActionResult Index()
    {
        return View();
    }


    public ActionResult Step1()
    {
        ViewBag.Hello = "Hello";

        return PartialView("_Partial1", ViewBag.Hello);
    }
}

Your view (Index.cshtml): 你的观点(Index.cshtml):

@Html.Action("Step1")

Your partial view (_Partial1.cshtml): 您的部分视图(_Partial1.cshtml):

@ViewBag.Hello
return PartialView("partialviewname", obj);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM