0

I wonder how to input @section in the partial view in Razor view. Is that possible or I must put it in a view only?

Thanks.

nam vo
  • 1
  • 2
  • I don't see why you can't - visual studio seems to let it happen, but how do you plan to force it to render? Are you trying to do nested partials or something? – Fourth May 24 '11 at 03:44
  • I want to put some script or css in different partialview, but i'm sure the section block in partialview doesn't work. – nam vo May 24 '11 at 04:08
  • @section CSS{ } >> won't work in partialview. It seems only normal views can use @section block – nam vo May 24 '11 at 04:13

2 Answers2

0

Why not try just creating a partial view that has all the javascript and css you want and just rendering that partial view in the layout? I don't think you need sections to get that done. The point to sections is to tell full views that they can have specific sections just for them and that the partial views won't force them to look like anything.

<!DOCTYPE html>
<html>
<head>
    <title>@ViewBag.Title</title>
    @Html.Partial("_HeaderLinksAndScripts")
</head>
<body>
    <div class="page">
        <div id="header">
            <div id="title">
                <h1>My MVC Application</h1>
            </div>
            <div id="logindisplay">
                @Html.Partial("_LogOnPartial")
            </div>
            <div id="menucontainer">
                @Html.Partial("_MenuPartial")
            </div>
        </div>
        <div id="main">
            @RenderBody()
        </div>
        <div id="footer">
            this is a footer
        </div>
    </div>
    @Html.Partial("_FooterScripts")
</body>
</html>

Just put the site-wide css and javascripts in the appropriate partial views.

Fourth
  • 9,163
  • 1
  • 23
  • 28
  • sure, that will work. I just want to elaborately put all script and css in the right parts of my Layout :) – nam vo May 24 '11 at 04:17
  • thanks for your explanation. maybe i didn't detail my question. I have a template(partial view) which will be shared to many Views. I don't want to put script/css on every views just for the same features and i'd like to put all script/css in the right parts in layout using @section – nam vo May 24 '11 at 04:42
  • http://stackoverflow.com/questions/5433531/using-sections-in-editor-display-templates/5433722#5433722 check here, thats probably more along the lines of what you are looking for. – Fourth May 24 '11 at 11:28
0

Put your scripts in that partial view not in a section (since you cant do @section in a partial) OR simply put them in your main layout and control their rendering conditionally.

Adam Tuliper
  • 29,982
  • 4
  • 53
  • 71