ViewModel in a masterpage? [closed]
I am using asp.net mvc 3.0 with razor.
I am wondering if I can make a view model for my master page. I have some dyn开发者_JAVA百科amic data in my master page so I am going to probably have to make a "base class" for all my controllers but I rather have strongly typed data then View Data/ View Bag.
So can this be done?
You could have all of your view models inherit from a base view model, which would include all of your common fields.
Alternatively, you could use a Custom Action Filter that would put some object in the view data which your master page would always expect to be there. This action filter could also be used to fill in the common fields in your view model base class.
i would suggest using RenderAction rather than having a base viewmodel from which all your view models will inherit. i guess viewmodel that you want to use in your master page is most probably going to contain navigational data (menu or leftbar etc.). if this is the case, RenderAction places clean separation of concerns and you can cache the data used in this particular action after loading it for first time and your further request will then be served from cached data.
精彩评论