views:

36

answers:

1

I have an ASP.NET MVC 2 project that renders conventional strongly typed pages, but on some of these pages I also want to use the page's object to render a specific area (div) in the navigation section of the Master Page.

For example, if I have a Client object and associated strongly typed View, I want the View to be able to insert client-specific navigation items within a div in the Master Page's navigation.

I've actually achieved this in a not very elegant fashion by creating a ViewData item to hold the Client object - this is in addition to returning the client object with the View. The Master Page then takes the ViewData object and, if it's not null, renders the client-specific navigation section. This way of doing things will get very ugly as the number of different objects that have to be handled increases.

What I am looking for is a method to update a placeholder (ie an empty div) in the Master Page navigation during the page rendering process. Any C#, VB.NET or jQuery solution welcome.

A: 

Why not use the master page/placeholder as you traditionally might with web forms? I'm not seeing how using MVC would dictate this being done another way.

View Masterpage:

<div id="menu">
    <div>Item1</div>
    <div>Item2</div>
    <asp:ContentPlaceHolder ID="MenuContent" runat="server" />
</div>

View:

<asp:Content ID="Content1" ContentPlaceHolderID="MenuContent" runat="server">
    <%=Html.RenderAdditionalMenuItemsSomehow(Model) %>
</asp:Content>
Kurt Schindler
A great suggestion (for which I had a doh! moment) and it works perfectly when I have the Client object available.However it's turned out that some of the items I need to display for the client won't include the client object, so now I'm looking at testing the ViewContext.Controller in the Master Page code to see if it's of the ClientController type and acting on that.
CrispinH