ASP.NET MVC 3, Razor Layout Controller-like?
Haven't found anything about how to do this. I Require some code to run to update data in my Razor Layout.开发者_StackOverflow
I could do this by running the code in every Action that is using the Layout and setting ViewBag data, but that seems impracticable...
So, how would i do this in a better way?
The answer to this question, proposes to use a global action filter.
That is a piece of code (filter) that is executed on every action, you can use it, to set properties in the ViewBag. These properties are accessible from every View, and from the layout.
If the data you are rendering in your layout is orthogonal to the data of your actual view (which it should be, or otherwise you are using layouts where you should be using partial views), then imho the conceptually most sound method would be to employ a child action (i.e. using Html.Action) to render the data into the layout.
精彩评论