2

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 dynamic 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?

chobo2
  • 83,322
  • 195
  • 530
  • 832
  • possible duplicate of [How to create a strongly typed master page using a base controller in ASP.NET MVC](http://stackoverflow.com/questions/768236/how-to-create-a-strongly-typed-master-page-using-a-base-controller-in-asp-net-mvc) – John Farrell Apr 19 '11 at 17:50

2 Answers2

4

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.

TJB
  • 13,367
  • 4
  • 34
  • 46
  • 2
    Just an FYI to @chobo2, the duped answer I linked to has a much better answer than this explaining all the pieces clearly. – John Farrell Apr 19 '11 at 18:17
0

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.

Muhammad Adeel Zahid
  • 17,474
  • 14
  • 90
  • 155