Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to add section Scripts to asp.net mvc Razor View without layout page

My mvc view layout is null. how can i add script inside view.

@{
   Layout = null;
}

@section Scripts{
<script type="text/javascript">

    $(document).ready(function () {
        $('#txtChild1').on('change', function () {
             $("#C1Age1").show();
        });
    });

</script>
}

Error show

cannot resolve section 'Scripts'

like image 566
sanjeewa Avatar asked Apr 20 '17 06:04

sanjeewa


People also ask

Where do you put JavaScript on a Razor page?

A JS file for the Index component is placed in the Pages folder ( Pages/Index. razor. js ) next to the Index component ( Pages/Index. razor ).

How do I use RenderSection in partial view?

You can render the partial view in the parent view using the HTML helper methods: @html. Partial() , @html. RenderPartial() , and @html. RenderAction() .


3 Answers

If you use Layout = null then you don't need to use @section Scripts any more. you can simply use script tag.

Example:

@{
   Layout = null;
}

<script type="text/javascript">
    $(document).ready(function () {
        $('#txtChild1').on('change', function () {
             $("#C1Age1").show();
        });
    });
</script>
like image 127
Ashiquzzaman Avatar answered Oct 02 '22 14:10

Ashiquzzaman


If you are not using layout page then you need not to add section. add scripts as you add them on a html page.

@{
   Layout = null;
 }
 <script type="text/javascript">

    $(document).ready(function () {
        $('#txtChild1').on('change', function () {
             $("#C1Age1").show();
        });
    });

</script>
like image 31
kritikaTalwar Avatar answered Oct 02 '22 12:10

kritikaTalwar


You can also add script block without section if it is not mandatory.

like image 24
Sandip - Frontend Developer Avatar answered Oct 02 '22 12:10

Sandip - Frontend Developer