开发者

ASP.NET MVC2 Master Page PlaceHolder visibility

开发者 https://www.devze.com 2023-04-01 22:03 出处:网络
asp:PlaceHolders have a visible property, and this controls whether their content is rendered on the page.

asp:PlaceHolders have a visible property, and this controls whether their content is rendered on the page.

I have declared a PlaceHolder in an MVC2 Master Page and set it's visibility to false.

Pl开发者_JS百科ease can you tell me how I can control the visibility of a PlaceHolder from within an MVC2 view template that inherits from an MVC2 master page?

This seems like a simple task, but I am struggling to see how it can be achieved. I think I should be able to get access to the PlaceHolder from within the descending view template (as with web forms code-behind) and just set it's visibility there but the way to do this is escaping me...


Don't do this. Placeholders are legacy from classic WebForms. Manipulating server side controls in an ASP.NET MVC application is very bad and you should never do it. So simply forget about setting properties on user controls. Fortunately in Razor placeholders have been completely removed and replaced by sections. So don't write code that you won't be able to migrate later.

One way to show/hide sections of your code in an ASP.NET MVC application is to use an if statement in your views. For example:

<% if (Model.ShouldShowSection) { %>
    <div>Some super section</div>
<% } %>

In this example we are testing a boolean value on the view model which the controller action that rendered this view would set.

0

精彩评论

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