How to pass viewmodel to a layout/master page?
Asked Answered
H

1

15

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 controllers' views. All the views are strongly-typed themselves... This layout page in fact visualizes some strongly-typed object. So, I need this object to be passed to the layout page to pass it to `Html.RenderPartial()' or to render it right in the page.

The article Passing Data to View Master Pages (C#) (though an old one for mvc2) gives an example to provide a base abstract controller class, passing the model to ViewData. This could be the cure.

But...

In some threads I found a way one could make a strongly-typed master page:

<%@ Master ... Inherits="System.Web.Mvc.ViewMasterPage<MyWeb.Models.Client>" %>

So, is there the same approach in mvc3? Or the only way to pass data to a layout page is to use ViewBag or ViewData?

Having read what is the best way to bind ViewModel data to master page in asp.net-mvc, I've considered Darin's answer, but still think that this bunch of views will have the same layout, and see no reason to go against DRY principle.

So, how to pass strongly-typed objects to layout pages? Or have I mixed something up?

Hysterics answered 18/10, 2012 at 2:17 Comment(0)
L
18

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.

Lat answered 18/10, 2012 at 2:50 Comment(9)
I wrote this some time back - avoid view bag. completedevelopment.blogspot.com/2011/12/… on the subject posted, use partial views or child actions and caching if required.Lop
This is just what I wanted, but was not sure to try with my own hands...While I started learning mvc with Sanderson S, Freeman A. - Pro ASP.NET MVC 3 Framework 3ed., I didn't find anything about passing models to layout. Though, as I see now, there is nothing straight preventing me from doing this. Thanks a lotHysterics
@AdamTuliper I do tend not to use ViewBag and ViewData, but was afraid of I have to..Hysterics
@Pure.Krome, does your meme say against using ViewBag/ViewData? Or I failed to catch it..Hysterics
Meme has nothing to do with ViewBag/ViewData. Just wanted to have a punch line at the end of the answer :)Lat
Anyhow, Pure.Krome answer works great in his case because the only reused variable is ClaimsUser and it is accessible from any controller with no context. In a more complex case, every controllers would need the list of variable in the parameter list (so that the ViewModel can be re-created), and every action link would require the parameters to be recopied. That make a lot of extra code everywhere. I think using the viewbag with a typed object for the layout view model is a simpler solution for this case.Presidio
@Presidio this solution does work great in any case. In my projects ViewModels are not that simple at all, and this solution does suit them...Using viewbag/viewdata is the last thing to use. When to use ViewBag, ViewData, or TempData in Mvc3Hysterics
@Presidio also ViewBag, ViewData and TempDataHysterics
Urgh at ViewBag, etc :( The master page usually has very little required data. In the above scenario (which is pretty common), is that the master page has a header, body and footer. therefore, the common stuff is data in the footer and header and maybe one or two things in the master. The bulk of each webpage is in the body section, which is very dynamic (each view is different to another). So there would be no common stuff to pass across to that. Anyways, this is answer is starting to get old, so whatever floats your boat. I just hate the ViewTeaBag with passion.Lat

© 2022 - 2024 — McMap. All rights reserved.