How to pass viewmodel to a layout/master page?

后端 未结 1 1630
谎友^
谎友^ 2021-02-07 11:29

Having googling for some time, I\'m a little bit confused with how to do this in asp mvc 3.

So, the task is to have a common layout (or master?) page for several control

1条回答
  •  天涯浪人
    2021-02-07 11:57

    It's an interesting debate topic -> passing view models to the master page / layout vs ViewBags.

    I'm -hate- using ViewBags so I have view models for all my view-layers. Starting with the _layout.cshtml and upwards. Any view that uses a Layout .. well .. that view model just inherits the Layout .. or whatever view is below it .. and this is repeated until you hit the bottom level which is usually the _layout...

    My RavenOverflow project has some sample code that shows this.

    • This is the the layout view, defined.
    • Here is the layout view model, defined.
    • This is the view that uses a layout.
    • Here is the view model, for that previous view.
    • This is some controller code, that creates the model .. which is then passed into the View.

    0 讨论(0)
提交回复
热议问题