Passing data to Master pages in ASP.NET MVC

Last post 04-02-2008 2:28 AM by maartenba. 17 replies.

Sort Posts:

  • Passing data to Master pages in ASP.NET MVC

    12-14-2007, 9:49 AM
    • Member
      3 point Member
    • uzmobile
    • Member since 12-14-2007, 12:30 PM
    • Posts 4
    Hi, I want the master page to have some dynamic data that will be displayed on all pages that use this master page. For example, I need to display links to latest blog posts on all pages. One obvious way is to pass that data in every acion's RenderView method but it's too repetitive. What is the recommeneded way to pass data to the master page? Please note that master and derived pages should recieve strongly typed data. Thanks!
    Filed under: , ,
  • Re: Passing data to Master pages in ASP.NET MVC

    12-14-2007, 11:45 AM
    Answer
    • Member
      14 point Member
    • scarpen
    • Member since 05-08-2007, 12:19 PM
    • Posts 4

    I'm curious what the recommended method is as well.

    For now I've created a "container" class that contains a property for the data needed by the specific view as well as properties for the data needed by the master page.  Then I created my own subclass of Controller and overrode the RenderView method to create this container and pass it along to the view.  That way the RenderView calls in the specific controllers still only concern themselves with passing data needed for that view.

    I've also used this technique for passing data to User Controls, but I'm curious if there is a better/recommended way.

  • Re: Passing data to Master pages in ASP.NET MVC

    12-14-2007, 12:32 PM
    • Contributor
      6,265 point Contributor
    • Haacked
    • Member since 09-17-2003, 2:43 PM
    • Posts 390
    • AspNetTeam
      Moderator

    @scarpen That's a nice way to do it for the CTP. We're looking at ways to make it more natural, but for now, what you did is what I would recommend.

    Phil Haack (http://haacked.com/)
    Senior Program Manager, Microsoft

    What wouldn’t you do for a Klondike bar?
  • Re: Passing data to Master pages in ASP.NET MVC

    12-14-2007, 1:06 PM
    Answer
    • Participant
      852 point Participant
    • robconery
    • Member since 02-23-2005, 10:16 PM
    • Posts 195

    You could create a UserControl and a "Service" that does this. The "Service" (call it "StatsService" for instance) would get all the information you need to show, the UserControl would render it, and you could add that UserControl to the MasterPage.

     

  • Re: Passing data to Master pages in ASP.NET MVC

    12-14-2007, 1:08 PM
    • Member
      378 point Member
    • shinakuma
    • Member since 03-01-2003, 4:09 PM
    • Posts 92

    scarpen:
    Then I created my own subclass of Controller and overrode the RenderView method to create this container and pass it along to the view.

    Does your RenderView override also know how to retrieve the data? That would feel alittle off.

    Looking at the requirement, it seems pretty obvious that retrieving a list of recent blog posts is a seperate action from whatever action you are performing. (Especially true if you work with RESTful applications). One quick solution is to turn that into a seperate AJAX call. Drawback is the complexity of managing the AJAX behavior and the extra workload on the server. Might not be suitable for everyone. Another way I can see this being addressed down the road in the next CTP drop is through some type of post action filter that allows you to specify additional "complimentary" actions to your main action. (I don't know what would be the right term to use here). That way we don't violate SoC, SRP and all that goodness.

    Now thinking about this, does RenderView actually spit out the html to the response stream when you call it or does it mark the view you are interested and delay the rendering to a later stage? That will effect what you can do in a post action filter.

  • Re: Passing data to Master pages in ASP.NET MVC

    12-14-2007, 1:39 PM
    • Member
      3 point Member
    • uzmobile
    • Member since 12-14-2007, 12:30 PM
    • Posts 4
    robconery:

    You could create a UserControl and a "Service" that does this. The "Service" (call it "StatsService" for instance) would get all the information you need to show, the UserControl would render it, and you could add that UserControl to the MasterPage.

    Ā 

    Would providing that information be the UserControl's own function? I mean, would the UserControl access that "service" in it's codebehind or something?
  • Re: Passing data to Master pages in ASP.NET MVC

    12-14-2007, 2:15 PM
    • Member
      3 point Member
    • uzmobile
    • Member since 12-14-2007, 12:30 PM
    • Posts 4

    scarpen:

    I'm curious what the recommended method is as well.

    For now I've created a "container" class that contains a property for the data needed by the specific view as well as properties for the data needed by the master page.  Then I created my own subclass of Controller and overrode the RenderView method to create this container and pass it along to the view.  That way the RenderView calls in the specific controllers still only concern themselves with passing data needed for that view.

    I've also used this technique for passing data to User Controls, but I'm curious if there is a better/recommended way.

    As I understand, container class has properties for all the views that controller's actions render. And specific actions fill out only  specific properties needed for the view, right?

  • Re: Passing data to Master pages in ASP.NET MVC

    12-14-2007, 2:21 PM
    Answer
    • Participant
      852 point Participant
    • robconery
    • Member since 02-23-2005, 10:16 PM
    • Posts 195

    uzmobile:
    Would providing that information be the UserControl's own function? I mean, would the UserControl access that "service" in it's codebehind or something?

    Yep - that's the idea. The service has the logic, the UI spins up the "view" of it. In Rails the UserControl would be called a "Partial" - a reusable bit of UI that you can put in various places.

  • Re: Passing data to Master pages in ASP.NET MVC

    12-14-2007, 3:16 PM
    • Member
      378 point Member
    • shinakuma
    • Member since 03-01-2003, 4:09 PM
    • Posts 92

    robconery:
    Yep - that's the idea. The service has the logic, the UI spins up the "view" of it. In Rails the UserControl would be called a "Partial" - a reusable bit of UI that you can put in various places.

    if the usercontrol calls this service directly (bypassing a controller piece), aren't you breaking MVC? rails partials don't call the model directly if I'm not mistaken. like the usercontrols, they rely on the page to pass them data needed.

  • Re: Passing data to Master pages in ASP.NET MVC

    12-14-2007, 5:29 PM
    • Member
      14 point Member
    • scarpen
    • Member since 05-08-2007, 12:19 PM
    • Posts 4

    uzmobile:

    As I understand, container class has properties for all the views that controller's actions render. And specific actions fill out only  specific properties needed for the view, right?

     

    Kind of, but not really.  The container class is actually generic for the type of data the view needs.  I couldn't see how to post code snippets in the forums, so I wrote this quick post that shows my current solution: http://geekswithblogs.net/scarpenter/archive/2007/12/14/117724.aspx

  • Re: Passing data to Master pages in ASP.NET MVC

    12-15-2007, 3:24 AM
    • Participant
      852 point Participant
    • robconery
    • Member since 02-23-2005, 10:16 PM
    • Posts 195

    shinakuma:
    if the usercontrol calls this service directly (bypassing a controller piece), aren't you breaking MVC?
     

    Yes and no. Yes in terms of the WebController concept - no in terms of a "ServiceController" concept. If you wanted to keep with the literal MVC here you could - you could have your control use something like Ajax or jQuery to call back to a "StatsController' and make a call to a List method that returns links. This is clean and nice - and works much the same way.

    I readily break rules to do what makes sense - Gravatar is an example of this. If you have an app that uses Gravatar, it has to go out to an external service after hashing a user's email, so the process has to go through a code layer in your app. This doesn't fit the MVC pattern exactly - but I think (my opinion) that this is OK, The Service model and MVC can fit together nicely.

    It's all up to you and what you think fits your scenario. The good thing is that there are ways to do it elegantly :). 

  • Re: Passing data to Master pages in ASP.NET MVC

    12-18-2007, 6:01 PM
    • Member
      574 point Member
    • abombss
    • Member since 06-27-2006, 4:13 PM
    • Chicago, IL
    • Posts 164

     Coming from Monorail they have a similar concept with their ViewComponents.  The component has a Initialize and Render method that can be overridden, so you can give your components/controls some logic

    Two things strike me as a smelly: 

    1) Code Behind.  I don't think there should be any, it will just get confusing and you will end with people doing some crazy stuff trying to mush webforms into mvc.

    2) User Controllers.  Out of the box there is no IoC so the default factory will new them up with Activator.Createinstance... bad, bad, bad

    So what would my solution be.

    I would create a ViewComponent base class for handling all my setup.  It would probably just have an Initialize method taking ControllerContext.  All my presentation logic would go in there so it is testable.  all my domain logic would be done in services that get injected into the constructor of my ViewComponent.

    I would have a base ViewComponentControl that simply used IoC to locate the correct ViewComponent class and Initialize it.  Now my UserControls just inherit from my ViewComponentControl, use the view like normal, and create a separate ViewComponent subclass to handle any setup, config, and presentation logic.

    I would probably run fxcop rules to ensure my code behind files were empty.

    Adam Tybor -- abombss.com
  • Re: Passing data to Master pages in ASP.NET MVC

    03-31-2008, 1:55 PM
    • Member
      158 point Member
    • [C.I.] Reman
    • Member since 05-22-2007, 6:52 AM
    • Germany, Dresden
    • Posts 44
    Is in the Preview 2 a new way to passing data to master pages or is the only solution (for now) to use a special control?
  • Re: Passing data to Master pages in ASP.NET MVC

    04-01-2008, 5:06 AM
    • Contributor
      7,054 point Contributor
    • rjcox
    • Member since 12-19-2007, 2:14 PM
    • Basingstoke, UK
    • Posts 1,444

    What's wrong with ViewData?

    (But see here if you want it strongly typed.) 

    Richard
  • Re: Passing data to Master pages in ASP.NET MVC

    04-01-2008, 5:21 AM
    • Member
      158 point Member
    • [C.I.] Reman
    • Member since 05-22-2007, 6:52 AM
    • Germany, Dresden
    • Posts 44

    The problem with viewdata is, that i only use viewdata on view pages.

    Each Controller has different Views (with there viewdata) - but the masterpage has no real "master-controller".

    That means, each controller must pass the "master-viewdata" for the masterpage - this isnĀ“t a great solution in my opionion.

Page 1 of 2 (18 items) 1 2 Next >