1

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

4
  • 3
    You'll probably want to use sections to achieve that. Commented Jul 17, 2012 at 10:29
  • @ManuLetroll: You really should have posted that as an answer. Commented Jul 17, 2012 at 10:56
  • @ManuLetroll - Yes ! The answer belongs to u dude ! Manu post it as a answer and Matthew should accept that one. Sorry i dint read through the comment and posted in . Commented Jul 17, 2012 at 11:02
  • In fact I intended to post an answer but for some reason I commented instead, I wanted to go into a bit more detail but didn't have the time right then. Please accept the existing answer :) Commented Jul 17, 2012 at 12:29

2 Answers 2

3

There are sections for this purpose.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks guys. I really should google it before I ask. I found the answer I was looking for seconds after submitting this post! Cheers.
@MatthewLayton - Great dude !! Accept the appropriate answer in that case . I would suggest Manu Letroll to answer it , And accept that !
1

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

1 Comment

Thanks V much...this is a great help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.