Miomir Dancevic
Miomir Dancevic

Reputation: 6852

Razor MVC render section

I am using master layout of Razor MVC that have something like this

@RenderSection("scripts", required: false)

And have partival view

_partial.cshtml

That have this

@section scripts
{
    @Scripts.Render("~/bundles/jquery")
}

And another partial in partial

_partial_inside_partial.cshtml

That also have

@section scripts
{
    <script>
       $('div').addClass('red');
    </script>
}

The problem i have this code inside partial, its load at he middle of the page, and jquery is at the bottom?

Upvotes: 10

Views: 26037

Answers (2)

el bayames
el bayames

Reputation: 21

I would place the @RenderSection("scripts", required: false) within the _layout.chtml head node as the last line. I know many people will disagree with me but I do it and so far I haven't got any issue by doing it. I use the "@RenderSection("scripts")" to place there my individual pages scripts. Some says it will increase or decrease the loading speed.

Upvotes: 0

Ashley Lee
Ashley Lee

Reputation: 3976

Sections don't work the same way in partial views. In order to achieve what you're after, your going to have to move your scripts to a file, include an HTML helper, then call that HTML helper to render your scripts for each partial view that is loaded.

Use this as a reference: Using sections in Editor/Display templates

Upvotes: 8

Related Questions