Mantorok
Mantorok

Reputation: 5266

ASP.Net MVC: Change ViewData in Child Action

I have the following in my _Layout.cshtml:

<title>@ViewData["PageTitle"]</title>

I then have a Child Action that is called, and I want to be able to set change this value in that controller action.

Is this possible?

Upvotes: 1

Views: 1601

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038730

You could try using the parent context. In your _Layout.cshtml:

<title>@ViewContext.ViewData["PageTitle"]</title>

and in your child action:

[ChildActionOnly]
public ActionResult Foo()
{
    ControllerContext.ParentActionViewContext.ViewData["PageTitle"] = "foo";
    return View();
}

Upvotes: 4

Related Questions