I got a view with Layout defined. In the layout, there is a section:
@RenderSection(\"JavaScript\", required: false)
In the view, based on a c
@section
blocks can only appear in markup contexts.
You can write
@if (condition)
{
<text>
@section JavaScript
{
<script type="text/javascript>
$(document).ready(function(){
//bla...
});
</script>
}
</text>
}
@if(condition) RenderSection(..)
@Section Javascript{ <script type="text/javascript> $(document).ready(function(){ //bla... }); </script> }
or in your layout:
@RenderSection(..)
and in your view:
@section Javascript{ if(condition) { <script type="text/javascript"> $(document).ready(function () { //bla... }); </script> } }
also see: Is there a way to make a @section optional with the asp.net mvc Razor ViewEngine?