开发者

Clean way to pass data to Master Page from View in ASP.NET MVC2 (set css class from view)

开发者 https://www.devze.com 2023-01-06 20:08 出处:网络
I have a ASP.NET MVC2 application with a master page. The master page renders the site layout divs as follows:

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 d开发者_运维技巧oes anyone have any good ideas?)


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>
0

精彩评论

暂无评论...
验证码 换一张
取 消