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

13,491

Solution 1

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>

Solution 2

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>

Solution 3

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

Share:
13,491
sanjeewa
Author by

sanjeewa

Welcome, my name is sanjeewa. I am a software engineer from sri lanka who loves to write software to build great products and help businesses succeed with their goals. I appreciate good design and I am seeing it's importance more then ever in todays apps, web sites and products.

Updated on June 05, 2022

Comments

  • sanjeewa
    sanjeewa almost 2 years

    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'