MVC3 assign variable on every page from single peice of code

Posted on 2011-05-04
Last Modified: 2013-11-08
Hi experts.

I'm new to MVC3 coming from an background.

I have a multi page size using razor views and a master layout page.

On the master page is a div that contains a banner image and what i wish to do is change that image depending on what page i go to.

So, what trying to find out is how to run code from the master page to populate a variable on the master page? In i would normally put query in the master page's .cs file and either push the variable to the front page or pull the variable from the code.

I realise that i could create a  public void and call this from the each pages action result, but I'm sure there must be a way to put the code in once and have it called automatically by each page.

Question by:fusioninternet
    LVL 9

    Expert Comment

    LVL 27

    Accepted Solution

    Create a base controller and have your controllers inherit from this basecontroller.
    in the constructor of the controller initialize a TempData varable with the path of the your image
    TempData["MyMasterImage"]="path to the image"
    reference the TempData["MyMasterImage"] in the master view

    LVL 5

    Expert Comment

    by:Vipul Patel
    <img src="@ViewData["masterImage"]" title="src="@ViewData["masterImage"]"" />
    Manipulate ViewData in the basecontroller suggested by sammy1971

    Create HTML helper class

    public static MvcHtmlString MasterImage(this HtmlHelper html)
       Action and Controller name can be found in the html object.
       Based on different action return image path and use it below.
            case "action1": imagepath = "\image1.jpg"; break
            case "action2": imagepath = "\image2.gif"; break
        var imgBuilder = new TagBuilder("img");
        imgBuilder.MergeAttribute("src", url.Content(imagePath));
        imgBuilder.MergeAttribute("alt", alt);
        string imgHtml = imgBuilder.ToString(TagRenderMode.SelfClosing);

        return MvcHtmlString.Create(imgHtml );

    use in master page

    LVL 1

    Author Closing Comment

    Yes basecontroller was the way to go and got it to work as i wnated once I managed to find out where to create a base controller and how to reference it

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Join & Write a Comment

    Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
    Recently I spent hours debugging an issue in a Rails project where ActiveRecord was causing MySQL errors trying to create a User object of a class at the top level of a Single Table Inheritance model structure.  It turns out `.create` behaves differ…
    The purpose of this video is to demonstrate how to set up the WordPress backend so that each page automatically generates a Mailchimp signup form in the sidebar. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now