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

  • image1825-439x194_thumb.png
    Change the WebDeploy Port or why do I need port 8172?

      If you use WebDeploy on a server operation system you would usually use Port 8172. But what is this Port for and can I change it? Hint: For installing the WebDeploy I recommend this Blogpost. Port 8172 = IIS Management Service Default Port The Port 8172 is the default port of the IIS Management ...

  • Windows Phone Fonts & what if Visual Studio lies

    Today I was confronted with a little Problem: my Windows Phone App refused to show me the Font I choose – also other thinks didn’t work. Although the Visual Studio Designer did show the Fonts: Unfortunately there isn’t much left in the Emulator: Reason for this: Windows Phone doesn’t include all the typos Windows does ...

  • Json-Online-Tools: Viewer & Json2Csharp generator

      Wherever APIs are mentioned the JSON format I not far away. Since I’m using two tools regularly I would like to introduce you to them. JSON Viewer If you only see the JSON-Text you are usually not able to see the structure. With the help of JSON Viewer you can have an easy overview: ...

  • Windows Phone SDK & „System“-Icons

      Although the Metro Design focuses a lot on Typography Icons are still quite important. If you install Windows Phone SDK you will receive 36 Icons. You can find them here: C:\Program Files (x86)\Microsoft SDKs\Windows Phone\v8.0\Icons Unfortunately many Icons you might know from the common applications are not integrated. Pedro Lamas extracted 99 additional Icons ...

  • image1830-570x194.png
    How can I figure out if my ADFS 2.0 works?

      I was working with ADFS 2.0 (“Active Directory Federation Services”) for a while when this simple question crossed my mind: How can I figure out if the connection between ADFS and AD “works”? Here is a simple test… What is ADFS? If you need some “position of trusts” beneath the AD-boarders you choose an ...

Support us