I am a intern student working on a project. We have the need to use a dynamic layout. Originally all that we needed was to know whether or not a search input should appear in the navbar and what catalog that should search on. Now there is more meta-info needed about the search and other dynamically displayed side bars.

All this information can be retrieved from contexts available in every page request so I was overriding the OnActionExecuted method in the projects baseController it looked some what like this.

    OnActionExecuted(ActionExecutedContext context)
        var controllerName = context.ActionDescriptor.ControllerDescriptor.ControllerName
        this.ViewBag.Things = getStuff(controllerName);
        this.ViewBag.OtherThings = getMoreStuff(contollerName);
        //.... about 15 more ViewBag variables....

Now in my layout, and any other page I can use all of these things. problem is the ViewBag is quickly becoming bloated, and I'm sure they will find more dynamic things to put in the layout. Also this seems to be promoting a somewhat ad-hoc approach that I'm not entirely comfortable with.

Instead I think I will move to using renderAction in the layout for these dynamic parts and forgo most of the ViewBag properties. Is there a major downside to using renderAction that I am not aware of? Am I over engineering and it is fine to stick with the ViewBag heavy approach?

I tried pushing back on having such a dynamic layout because it seems like a weird design choice but they insist on having it, so I am curious as to best approach to such a thing. My current approach is working but seems bad for maintainability and growth of the site.


If every view needs common data, create a child class of System.Web.Mvc.WebViewPage that has the common stuff for each page:

public abstract class BasePageView<T> : System.Web.Mvc.WebViewPage<T>
    // the common stuff goes here

And then use it in your Razor Views:

@inherits BasePageView<YourViewModel>

  • Not every view needs common data. This layout needs common data. We have a few different layouts being used. student teacher admin etc.... The question is since there are a lot of variables needed should we create actions and call render action in the layout or is it acceptable to just pack a ton of ViewBag properties to be used in the layout. Thank you for the response, but I don't think this answer is appropriate for this situation. – Tenderdude Dec 21 '16 at 22:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.