Razor RenderSection within script tags – How to insert script from view into template function

You don’t need the single quotes around the RenderSection call in your layout:

<script type="text/javascript">
    $(document).ready(function () {
        @RenderSection("DocumentReady", false)
    });
</script>

and inside the view:

@section DocumentReady {
    alert('');
}

But it will probably be more readable if you have a scripts section in your layout:

@RenderSection("Scripts", false)

and inside the view:

@section Scripts {
    <script type="text/javascript">   
        $(function() {
            alert('');
        });
    </script>
}

Leave a Comment