views:

48

answers:

1

I have a ASP.NET MVC2 application with a master page. The master page renders the site layout divs as follows:

<div id="wrapper">

  <div id="column1">
    {contentplaceholder}
  </div>

  <div id="column2">
    {contentplaceholder}
  </div>

</div>

In my View, I would like to apply a classname to the wrapper div, so when viewing the homepage, the wrapper div would be:

<div id="wrapper" class="homepage">
</div>

or on the contact page it would be

<div id="wrapper" class="contact">
</div>

Ideally I would like to set this variable in the view aspx page, rather than in a controller action. What would be the cleanest way to achieve this? I was thinking something along the lines of:

In Master page:

<div id="wrapper" class="<%=WRAPPER_CLASS%>">
</div>

and then in View:

<% WRAPPER_CLASS = "contact"; %>

(obviously the above example doesn't work, but does anyone have any good ideas?)

A: 

Why not try this, within the master page:

<div id="wrapper" class="<asp:ContentPlaceHolder ID="page-class" runat="server" />">

</div>

and in the aspx view

<asp:Content ID="page-class-content" ContentPlaceHolderID="page-class" runat="server">
    homepage
</asp:Content>
Ahmad
or http://stackoverflow.com/questions/2593534/pass-data-to-master-page-with-asp-net-mvc
Ahmad
I actually did think about using another content placeholder to render the classname into the master page, but it seemed a bit overkill. Perhaps it may be the way to go - if nobody else can offer a better alternative solution, I'll mark yours as the accepted answer.
Astrofaes
@Astrofaes - there are other alternatives such as creating your own custom masterpage. There was a recent SO question (http://stackoverflow.com/questions/3215134/anyone-using-a-custom-viewmasterpage) which may provide some further ideas, however my feeling is do the simplest thing for now and get you functionality working and if you need to expand and get creative you can always do so at a later stage.
Ahmad