bundling datepicker in asp.net

Hi Experts,

      I had tried to included reference to bootstrap-datepicker to my index.html.

I get the following errors in chrome developer tools.

Uncaught TypeError: Cannot read property 'fn' of undefined
Uncaught ReferenceError: $ is not defined

Please find the attached index.html copied from the chrome developer tool below,

I dont want to use cdn.

Please help me how to resolve this issue.

TIA,

Regards,

Bharath. A. K
index.html
LVL 1
Bharath A.KAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Lokesh B RDeveloperCommented:
Hi,

Add the jQuery scripts before the date picker scripts.

    <script src="/Scripts/jquery-1.10.2.js"></script>

    <script src="/Scripts/bootstrap.js"></script>
<script src="/Scripts/respond.js"></script>
<script src="/Scripts/mybundle/bootstrap-datepicker.js"></script>
<link href="/Content/mybundle/bootstrap-datepicker.css" rel="stylesheet" />

Open in new window

0
CtrlAltDlCommented:
Your $document.ready() needs to be after you load your jquery script.

<link href="/Content/mybundle/bootstrap-datepicker.css" rel="stylesheet" />
<link href="/Content/bootstrap.css" rel="stylesheet"/>
<link href="/Content/site.css" rel="stylesheet"/>

<script src="/Scripts/jquery-1.10.2.js"></script>
<script src="/Scripts/bootstrap.js"></script>
<script src="/Scripts/respond.js"></script>
<script src="/Scripts/modernizr-2.6.2.js"></script>
<script src="/Scripts/mybundle/bootstrap-datepicker.js"></script>
<script>
        $(document).ready(function () {
            $('#datepicker').datepicker({
                format: "dd/mm/yyyy",
                todayBtn: "linked",
                autoclose: true,
                todayHighlight: true
            });
        });
</script>

Open in new window

0
Bharath A.KAuthor Commented:
Hi Expert,

          I had already bundled in bundle config.cs,  Why to bundle the script twice,
It has one base layout page and child view razor view.  Date picker is in child view or template.

Please help me how to resolve this.

Code in bundleconfig.cs is
     public static void RegisterBundles(BundleCollection bundles)
        {
            bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
                        "~/Scripts/jquery-{version}.js"));

            bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
                        "~/Scripts/jquery.validate*"));

            // Use the development version of Modernizr to develop with and learn from. Then, when you're
            // ready for production, use the build tool at http://modernizr.com to pick only the tests you need.
            bundles.Add(new ScriptBundle("~/bundles/modernizr").Include(
                        "~/Scripts/modernizr-*"));

            bundles.Add(new ScriptBundle("~/bundles/bootstrap").Include(
                      "~/Scripts/bootstrap.js",
                      "~/Scripts/respond.js"));
           
           /* bundles.Add(new ScriptBundle("~/bundles/mybundle").Include(            
                      "~/Scripts/mybundle/bootstrap-datepicker.js",
                      "~/Scripts/mybundle/app.js"));*/

            bundles.Add(new StyleBundle("~/Content/css").Include(
                      "~/Content/bootstrap.css",                    
                      "~/Content/site.css"));

            /*bundles.Add(new StyleBundle("~/Content/mybundle").Include(
                       "~/Content/mybundle/bootstrap-datepicker.css",
                       "~/Content/mybundle/app.css"));*/

code in _Layout.cshtml is

<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>@ViewBag.Title - My ASP.NET Application</title>
    @Styles.Render("~/Content/css")
    @Scripts.Render("~/bundles/modernizr")

</head>
<body>

     .......


    <div class="container body-content">
        @RenderBody()
        <hr />
        <footer>
            <p>&copy; @DateTime.Now.Year - My ASP.NET Application</p>
        </footer>
    </div>

    @Scripts.Render("~/bundles/jquery")
    @Scripts.Render("~/bundles/bootstrap")
    @RenderSection("scripts", required: false)

Then there index.cshtml and child view inside index.cshtml
code in child view (basic information.cshtml)


<script src="~/Scripts/mybundle/bootstrap-datepicker.js"></script>
<link href="~/Content/mybundle/bootstrap-datepicker.css" rel="stylesheet" />

   @Html.ValidationSummary("", new { @class = "text-danger" })
    <script>
        $(document).ready(function () {
            $('#datepicker').datepicker({
                format: "dd/mm/yyyy",
                todayBtn: "linked",
                autoclose: true,
                todayHighlight: true
            });
        });
    </script>
Please help me to resolving this issue.
With Many thanks,

Bharath. A.K
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Bharath A.KAuthor Commented:
Hi Expert,

       When I add Jquery reference, once again,  i.e two jquery reference one at bundleconfig.cs and other above the child view(CSHTml) I get the following error in my developer tools.

The error is

"TypeError: $(...).datepicker is not a function"

Please help in resolving this issue.

With Many Thanks,

Bharath. A. K
0
Lokesh B RDeveloperCommented:
Hi,

You need to keep this line at the top of the Layout Page.

    @Scripts.Render("~/bundles/jquery")

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Bharath A.KAuthor Commented:
Thanks, it is now working fine

Regards,

Bharath. A.K
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.