There is no clear cut mechanism provided by the ASP.NET MVC framework to pass data from a view to it’s master page. I’ve been using a technique that I’m reasonably happy with, which I’ll describe in this blog post.
For starters, my example will be using the ITypeInstanceDictionary provided in the TTC Tools. Please read my blog post pass data from an ASP.NET MVC controller to view to learn more. The code shown in this blog post is included in a demo web application on the TTC Tools GitHub page.
There are many scenarios in which you want to drive master page values from the view. For example: some CSS styling that exists in the master page will be dependent on the view. Personally I feel this should not be driven by the controller, as there are many situations in which CSS styles are purely display driven.
I’ll summarize the basic concepts for passing data from the view to it’s master page.
My example code will show how to override the pages body element id in the master page from a view.
The following code snippet shows an example master page implementation:
The view can override master page view data by creating a content control for the master page’s content place holder which follows the setup of the master page’s view data.
The ASP.NET MVC view engine rendering execution flow will start at the top of the master page and then flow into the content place holders. This allows the view to override the master page view data before it is used later in the master page markup code.
I created a screen cast which might help explain this technique in a bit more detail.
If you need any further explanation or have any questions please let me know. I hope someone finds this useful :-)
Next entry: Moq – Use Setup arguments (parameters) in the Returns of a mocked function
Previous Entry: Pass data from an ASP.NET MVC Controller to the View using the TTC Tools type instance dictionary
Latest entries:
Create absolute URLs using ASP.NET MVC
Comments
My Links
Tags
Follow me
About
Powered by FoxBlog
Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.
© Copyright 2011, Nathan Fox