ASP.NET MVC: Display dynamic data on master pages

If you play with the MVC 2 bits for a while you will come to a very simple question: How can I display dynamic data on my master page?

The structur of an MVC App (this is my demo app) :

image

Each controller has it´s own View-Folder and render the specific view (EntryController route to Views\Entry\VIEW).

But if you try to add a dynamic control (a dynamic menu, news-ticker, overviews…) on a master page you have a problem: A master page has no real controller itself (maybe a master page could have a controller – but for this problem there is a very easy way to do this).

The CTP 2 includes a special controller – the "ComponentController":

I use the "ComponentController" in my demo app to display the whole category list and the tagcloud.

My "EntrySideBarController" inherit from "ComponentController"

    public class EntrySidebarController : ComponentController
    {
        public void CategoryList()
        {
            ... viewdata = ...
            RenderView("CategoryList", viewData);
        }

        public void TagCloud()
        {
            ... viewdata = ...
            RenderView("TagCloud", viewData);
        }
    }

The views "CategoryList" & "TagCloud" are located in a special folder (root\Components\CONTROLLERNAME\Views\VIEWNAME):

image 

To render these components just use the "Html.RenderComponent" method (is similar to a usercontrol) :

    <%=Html.RenderComponent<EntrySidebarController>(component => component.CategoryList())%>
    <%=Html.RenderComponent<EntrySidebarController>(component => component.TagCloud())%>

Result:

image 

For more information about the ComponentController, look at this post:

Using the ComponentController in ASP.NET MVC CTP 2

If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.

About the author

Written by Code Inside Team

Currently there is no additional info about this author.

2 Responses

  1. RenderComponent seems gone in MVC Beta. Which method has been created to do the same?

    Reply
  2. The RenderComponent (or how it´s named in the actual version of ASP.NET MVC) is gone to the ASP.NET MVC Future – just link the future DLL.

    Reply

Comment on this post

Recent Posts

  • Automated Security Analyser for ASP.NET websites

    Evil Hackers are lurking everywhere and many Web-applications are delicately and share “too much” with the attacker. A quick (first!) overview offers the Tool “ASafaWeb”. All the website does is making a few requests and writing an Analyses including problem solving’s. There are no permanent disadvantages (bad requests/ DoS attacks and so on). Example: KnowYourStack.com ...

  • image1489-570x194.png
    „Sign in with Twitter“ for your own ASP.NET WebApp

      “Sign in with Twitter” is a popular practice to authenticate the users on your website. One advantage compared to an own registration is the lower inhibition for the user. But on the other hand Twitter doesn’t fess up with all the information’s and you will get into a kind of addiction. At the end ...

  • image1485-570x194_thumb.png
    CodePlex is going to be updated

      CodePlex the Microsoft Open Source Project Hosting Plattform hasn’t changed that much in the last few years and for a few times I thought Microsoft stopped the whole developing process. But now I found out that there is still life in the project. Maybe it is because of the success of GitHub or because ...

  • image1474_thumb.png
    What does Adobe in the flash-free web? Magazine-Style Layouts with CSS Regions!

      Adobe is well known for Photoshop and Flash but of course there is a lot more. According to the “Future Post” from Google Adobe declared one of their big subjects on a Blogpost. I’m talking about the W3C Working Draft to CSS Regions. Adobe cooperates with the WebKit Team and W3C on this. What ...

  • image1471-523x194.png
    HTML 5 Games, Tooling & 3D

      Game Developing is an interesting subject for all kind of software developer. But as a web developer without any Flash-skills there aren’t that much starting points. With HTML5 and the combination between Javascript, CSS3 and fast browsers there are the first “robust” HTML5 games. HTML5 games? Is this real? Neowin created a “Top 10” ...

Support us