Search code examples
c#htmlasp.netrazorrazorengine

Razor view engine pass data from layout to content


I am using razor view engine outside of mvc framework. Before you wonder, there are some project specific reasons.

So While adding pages, I add .cshtml layout and content page. The question is without mvc framework(which takes out possibility of viewback,tempdata) , How can I access variable/data on layout page in content cshtml page? For example: If I have in layout

@data=DB.GetData()

Then Can I have @data accessible in content .cshtml page?


Solution

  • You can use the PageData dictionary:

    @{PageData["data"] = DB.GetData();}
    

    or if you are a fan of dynamic stuff:

    @{Page.data = DB.GetData();}