Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • ThomasBrunbjerg 90 posts 182 karma points
    Sep 07, 2017 @ 06:41
    ThomasBrunbjerg
    0

    Adding script tags on child templates

    I have a child template in my MVC project where I want to initialize a Javascript framework. This should not be done on the master template, as I need some razor code on the page to work with the framework. The framework in question is Bootstrap-Year-Calendar, which I need to have talk to an SQL Database. Each child template will receive different data, so I cannot put the code on the master template for this. But if a place a script tag on the child template with the code to initialize the framework:

    $('#calendar').calendar({
            displayWeekNumber: true,
            customDayRenderer: function (element, date) {
                $(element).css('background-color', 'red');
                $(element).css('color', 'white');
                $(element).css('border-radius', '15px');
            }
        });
    

    But when I put the script tag with the code on my child template I receive this error:

    TypeError: $(...).calendar is not a function

    I have the .js and .css files included n my master template, so they should be available to use on the child template. My project was made with Umraco CMS as well.

    How can I display the calendar on my child templates?

  • Aristotelis Pitaridis 84 posts 402 karma points
    Sep 07, 2017 @ 07:43
    Aristotelis Pitaridis
    1

    Hello Thomas,

    Your problem is that in your final html the jQuery code is before your jQuery librari definition. In your master template type the following line at the location you want the script to be placed (after your jquery definition):

    @RenderSection("scripts", required: false)
    

    In you child template type the following code:

    @section scripts{
        <script type="text/javascript">
            $(function () {
                // Here you can place your jquery
            });
        </script>
    }
    

    Kind Regards,

    Aristotelis

  • Laurent Lequenne 122 posts 247 karma points
    Sep 07, 2017 @ 07:43
    Laurent Lequenne
    0

    Have a look on how to work with Razor and sections :-)

Please Sign in or register to post replies

Write your reply to:

Draft