Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8454379
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T12:00:51+00:00 2026-06-10T12:00:51+00:00

I am using MVC4 and I have a question, rather than declaring ViewBag.MenuLevel=twostage; in

  • 0

I am using MVC4 and I have a question, rather than declaring ViewBag.MenuLevel=”twostage”; in EACH ActionResult in my controllers can I declare it ONCE for each controller somehow? Right now for example I have lets say 40 ActionResults per controller, this seems silly to have to declare a variable that many times – so I am sure I am missing something simple.

I need to be able to consume it in my View somehow to affect presentation. Thoughts? Models? Kind of new to MVC so be gentle! 😉

Basically I want each section (controller) to display a different background-color on the front-end so I want to print the section/controller to an id in the html so I can control via css.

Cheers.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T12:00:53+00:00Added an answer on June 10, 2026 at 12:00 pm

    You are going to have to set the ViewBag data each time, that’s an unavoidable feature of the architecture. However, you can make it a little cleaner in terms of controller code with an attribute applied to the controller.

    public class ViewBagAttribute : ActionFilterAttribute
    {
        public void OnActionExecuting(ActionExecutingContext filterContext)
        {
            var controller = filterContext.Controller;
    
            controller.ViewBag.MenuLevel = "twostage";
        }
    
        public void OnActionExecuted(ActionExecutedContext filterContext) { }
    }
    

    If you apply that attribute to the controller, you won’t have to explicitly add the ViewBag code for each Action method, as OnActionExecuting is called automatically for every Action method.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a web site built using ASP.NET MVC4 that I can publish fine
I've created a method using the new WebAPI features in MVC4 and have it
I have been writing an application using ASP.Net MVC4, where the majority of the
I've got really used to developing views in MVC4 using Razor. But I have
I'm using VS 2010 Premium. I have a MVC4 project using SqlCe 4.0 with
I created a website using ASPNET.MVC4 RC. For each view I created 2 files
I'm using the new Bundling feature in ASP.NET MVC4. Currently I have the following
I have an asp.net MVC4 application that I am using Unity as my IoC.
I have created a Single Page Application (ASP.NET MVC4) using Steven Sanderson's instructions .
I am building a POC using MVC4. I have some jquery json calls to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.