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>
}
For example, in your _layout.cshtml :
@RenderSection("JavaScript", required: false)
And then in your view :
@section JavaScript
{
<script type="text/javascript" src="@Url.Content("/Scripts/SomeScript.js")"></script>
<script type="text/javascript" src="@Url.Content("/Scripts/AnotherScript.js")"></script>
<script type="text/javascript">console.log("in the js");</script>
}
Hope that helps