2

I have layout page and the page that uses layout. How can I add some new elements to the head not changing layout.(layout already contains head)?

Layout:

<head>...</head>

I want my page be like:

<head>all layout head logic... plus
     my page new elements...
     </head>
1
  • Sorry, this is not clear Commented Nov 4, 2011 at 19:18

1 Answer 1

3

You could use sections in the layout. For example:

<html>
<head>
    @RenderSection("scripts", false)
</head>
<body>
    @RenderBody()
</body>
</html>

and then in the view override this section and provide contents for it:

@section scripts {
    <script type="text/javascript">
        alert('hello');
    </script>
}

<div>Hello from the index view</div>

And since the section is optional (second argument = false) if a view doesn't provide any contents for it, it will stay empty.

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

Comments

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.