Antaris/RazorEngine — Issue #7: Share state between view and Layout view?
Author: sfmskywalker
Created: 2011-11-26 20:32:28+00:00
State: open

I would like to be able to set some variable from within a content view, which gets rendered by its Layout view.
For example, in Razor for MVC I can do this: ViewBag.Title = "My Content Page".
Then, from within Layout.cshtml I can render that string like so:

@ViewBag.Title

I tried to mimic that behavior by setting a custom base template which has a dynamic ViewBag property initialized to a new ExpandoObject, but it looks like as if the Layout template gets executed before the content template itself (causing a "Cannot perform runtime binding on a null reference" error, probably because Title is not yet set from the content page)