Modifying MVC 3 ViewBag in a partial view does not persist to the _Layout.cshtml

后端 未结 10 1861

I am using MVC 3 with the Razor view engine. I want to set some values in the ViewBag inside a Partial View and want retrieve those values in my _Layout.cshtml. For exampl

10条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-08 14:10

    I this is what page data is designed for. Pop this into your view.

    @Page.somePropertyName = "Whatever you want";
    

    And then access it in your layout view. Be sure to check that its not null first.

    @{
        if(Page.somePropertyName != null)
        {
           //Do stuff
        }
    }
    

提交回复
热议问题