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!
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.
Marked as answer by uzmobile on Dec 14, 2007 06:25 PM
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.
Marked as answer by uzmobile on Dec 14, 2007 06:24 PM
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.
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?
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?
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.
Marked as answer by uzmobile on Dec 14, 2007 06:24 PM
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.
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
uzmobile
Member
3 Points
4 Posts
Passing data to Master pages in ASP.NET MVC
Dec 14, 2007 01:49 PM|LINK
MVC Views Master Pages
scarpen
Member
14 Points
4 Posts
Re: Passing data to Master pages in ASP.NET MVC
Dec 14, 2007 03:45 PM|LINK
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.
Haacked
Contributor
6901 Points
412 Posts
Re: Passing data to Master pages in ASP.NET MVC
Dec 14, 2007 04:32 PM|LINK
@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.
Senior Program Manager, Microsoft
What wouldn’t you do for a Klondike bar?
robconery
Participant
852 Points
195 Posts
Re: Passing data to Master pages in ASP.NET MVC
Dec 14, 2007 05:06 PM|LINK
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.
shinakuma
Member
378 Points
92 Posts
Re: Passing data to Master pages in ASP.NET MVC
Dec 14, 2007 05:08 PM|LINK
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.
uzmobile
Member
3 Points
4 Posts
Re: Passing data to Master pages in ASP.NET MVC
Dec 14, 2007 05:39 PM|LINK
uzmobile
Member
3 Points
4 Posts
Re: Passing data to Master pages in ASP.NET MVC
Dec 14, 2007 06:15 PM|LINK
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?
robconery
Participant
852 Points
195 Posts
Re: Passing data to Master pages in ASP.NET MVC
Dec 14, 2007 06:21 PM|LINK
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.
shinakuma
Member
378 Points
92 Posts
Re: Passing data to Master pages in ASP.NET MVC
Dec 14, 2007 07:16 PM|LINK
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.
scarpen
Member
14 Points
4 Posts
Re: Passing data to Master pages in ASP.NET MVC
Dec 14, 2007 09:29 PM|LINK
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