EKsumic's Blog

The current theme of this website is Japanese learning.

Click the left button to use the catalog.

OR

[ .NET Core 3.1 MVC ] What is @section scripts{}? And how to use it?

What is @section scripts{}? And how to use it?

Your section scripts may like this:

@section scripts{
    <script type="text/javascript">
        function load() {
            $.get('@Url.Action("Results")?teamcode=' + teamcode + '&orderby=' + orderby+'&page=1&size=10', function (data) {
                $('#results').html(data);
            })
        }
    </script>
}

It's placed in a view page.

If you run it directly, it will report an error.

Yes, if you write like this, you must add a @RenderSection("scripts", required: false) to the layout page.

This part of your javascript will become part of the layout page.

The main advantage of writing this way is to make the layout page look more concise.

As a reminder, you can write @RenderSection("scripts", required: false) directly on the layout page, and you don't need to write @section Scripts{} in the sub-page, but if you write @section Scripts{} in the sub-page, then You must write @RenderSection("scripts", required: false) on the layout page, otherwise the same error as mentioned above will be reported.

This article was last edited at 2020-10-29 21:31:01

* *