1

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?

1 Answer 1

4

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();
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks very much, I was nearly there but was missing the @ViewContext bit :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.