What the preferred way to add view-specific JavaScript to an Asp.Net Core 2 MVC application? - Stack Overflow

I have JavaScript that I'd like to add to a single Razor view (CSHTML), which should be associated

I have JavaScript that I'd like to add to a single Razor view (CSHTML), which should be associated with an input that filters a TABLE:

$(document).ready(function () {

    // added for testing
    alert('ready');

    (function ($) {

        $('#filter').keyup(function () {

            var rex = new RegExp($(this).val(), 'i');
            $('.searchable tr').hide();
            $('.searchable tr').filter(function () {
                return rex.test($(this).text());
            }).show();

        })

    }(jQuery));

});

At the moment, the code is contained in a SCRIPT tag located at the bottom of the view.

When the page loads, the alert never fires.

If I wrap the SCRIPT:

@section Head {
    <script type="text/javascript">
        $(document).ready(function () {
            // your code goes here
        });
    </script>
}

this error is produced:

InvalidOperationException: The following sections have been defined but have not been rendered by the page at '/Views/Shared/_Layout.cshtml': 'Head'. To ignore an unrendered section call IgnoreSection("sectionName").

What the preferred way to add view-specific JavaScript to an Asp.Net Core 2 MVC application?

relevant:

I have JavaScript that I'd like to add to a single Razor view (CSHTML), which should be associated with an input that filters a TABLE:

$(document).ready(function () {

    // added for testing
    alert('ready');

    (function ($) {

        $('#filter').keyup(function () {

            var rex = new RegExp($(this).val(), 'i');
            $('.searchable tr').hide();
            $('.searchable tr').filter(function () {
                return rex.test($(this).text());
            }).show();

        })

    }(jQuery));

});

At the moment, the code is contained in a SCRIPT tag located at the bottom of the view.

When the page loads, the alert never fires.

If I wrap the SCRIPT:

@section Head {
    <script type="text/javascript">
        $(document).ready(function () {
            // your code goes here
        });
    </script>
}

this error is produced:

InvalidOperationException: The following sections have been defined but have not been rendered by the page at '/Views/Shared/_Layout.cshtml': 'Head'. To ignore an unrendered section call IgnoreSection("sectionName").

What the preferred way to add view-specific JavaScript to an Asp.Net Core 2 MVC application?

relevant: https://stackoverflow./a/24895364/134367

Share Improve this question asked Oct 20, 2018 at 3:56 craigcraig 26.3k28 gold badges130 silver badges220 bronze badges 2
  • 1 Duplicate of stackoverflow./questions/45796556/… – mason Commented Oct 20, 2018 at 4:12
  • Possible duplicate of The following sections have been defined but have not been rendered for the layout page “~/Views/Shared/_Layout.cshtml” – hyvte Commented Oct 22, 2018 at 6:09
Add a ment  | 

1 Answer 1

Reset to default 4

Found the answer.

I wrapped my JAVASCRIPT in a @section Scripts block:

@section Scripts {
    <script type="text/javascript">
    $(document).ready(function () {

        alert('ready');

        (function ($) {

            $('#filter').keyup(function () {

                var rex = new RegExp($(this).val(), 'i');
                $('.searchable tr').hide();
                $('.searchable tr').filter(function () {
                    return rex.test($(this).text());
                }).show();

            })

        }(jQuery));

    });
    </script>
}

This appears to be called by @RenderSection("Scripts", required: false) in the _Layout.cshtml.

Once I did this and reloaded the page, the alert fired and the remainder of the code worked as expected.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745625520a4636779.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信