Reputation: 42260
I am porting a classic ASP.NET application (ASPX pages) to ASP.NET MVC 3. Currently I use ContentPlaceHolder(s) to dynamically add content to specific ASP.NET ASPX pages. Consider the following:
<!DOCTYPE html>
<html>
<head>
<link href="MyStyleSheet.css" rel="Stylesheet" type="text/css" />
<asp:ContentPlaceHolder ID="Styles">
</asp:ContentPlaceHolder>
<script type="text/javascript" src="jQueryAndOtherScripts.js"></script>
<asp:ContentPlaceHolder ID="Scripts">
</asp:ContentPlaceHolder>
</head>
<body>
<div id="Container">
<asp:ContentPlaceHolder ID="Content">
</asp:ContentPlaceHolder>
</div>
</body>
</html>
So in the snippet above I have 3 content place holders so I can add addition styles, scripts and content based on the individual page. The reason I have done it this way is so that each page only loads the styles, scripts relevant to the content on the page (for example if it was the login page, I might add a script to check that the login details are valid)
Consider the following MVC 3 Razor View snippet:
<!DOCTYPE html>
<html>
<head>
<link href="MyStyleSheet.css" rel="Stylesheet" type="text/css" />
<script type="text/javascript" src="jQueryAndOtherScripts.js"></script>
</head>
<body>
<div id="Container">
@RenderBody()
</div>
</body>
</html>
Now for the problem...RenderBody() replaces <asp:ContentPlaceHolder ID="Content"></asp:ContentPlaceHolder>
, but what do I use as a replacement for <asp:ContentPlaceHolder ID="Styles"></asp:ContentPlaceHolder>
and <asp:ContentPlaceHolder ID="Scripts"></asp:ContentPlaceHolder>
?
Upvotes: 1
Views: 1250
Reputation: 3802
Sections
are the way to achieve this. Check out this excellent post by ScottGu on the subject.
http://weblogs.asp.net/scottgu/archive/2010/12/30/asp-net-mvc-3-layouts-and-sections-with-razor.aspx
Upvotes: 1