Search code examples
asp.net-mvc-4razor-2

Is it possible pass a string from the _ViewStart file to my Layoutfile?


I have a Layout File that is used by both my main site and the Admin area. In the Layout I make a call to the MVCSiteMapProvider to generate my layouts. What I would like to do is setup a string in the _ViewStart file that I can pass into the Layout to designate which sitemap to use. Is it possible to pass variables from the _ViewStart down the Layout file and if so how?


Solution

  • Just off the top of my head you could use PageData.

    Provides array-like access to page data that is shared between pages, layout pages, and partial pages.

    _ViewStart

    C#

    @{
        PageData["MyString"] = "Test!";
    }
    

    VB.NET

    @Code
        PageData("MyString") = "Test!"
    End Code
    

    _Layout

    C#

    @PageData["MyString"]
    

    VB.NET

    @PageData("MyString")