0

I have the following Editor Template defined and when I call it from my View it is throwing the Uncaught ReferenceError: $ is not defined error.

@model System.DateTime?

<input id="@ViewData.ModelMetadata.PropertyName" title="datepicker" />

<script type="text/javascript">
    $("#@ViewData.ModelMetadata.PropertyName").kendoDatePicker();
</script>

In my view I call the editor template as follows

@Html.EditorFor(m => m.DateHired)

I have tried to put the code into a Script section but it is still giving the same error.

@section Scripts
{
    <script>
        $(document).ready(function () {
            $("#@ViewData.ModelMetadata.PropertyName").kendoDatePicker();
        });
    </script>
@
  • Thank you for the suggestion but it does not resolve the issue because the javascript code is called within an EditorTemplate and that this EditorTemplate is called from a View. I am not sure what is the proper way to have the javascript code work as my view already contains reference to the jquery library. – user14322615 Aug 25 '21 at 18:29
  • When and where exactly is that error being thrown? Also, your last piece of code wouldn't work since it's commented out – Camilo Terevinto Aug 25 '21 at 18:42
  • Make sure jquery is in the `` not at the bottom just before `` – freedomn-m Aug 25 '21 at 18:42
  • *my view already contains jquery* - why does your *view* have jquery and not your layout? – freedomn-m Aug 25 '21 at 18:42
  • $user14322615: Does the error is thrown on `$("#@ViewData.ModelMetadata.PropertyName").kendoDatePicker();` line? – Jackdaw Aug 25 '21 at 18:43
  • @freedomn-m jquery has no need to go in the head, it's perfectly fine for it to go in the body – Camilo Terevinto Aug 25 '21 at 18:44
  • The duplicate has 45 answers, and the accepted answer lists at least three reasons why the error may be occurring. The question does not have enough information to reproduce the error (the relative locations of the `script` elements that introduce jQuery and that use jQuery), and no indication that any of the 45 answers have been examined or attempted. If you have, [edit] your question with the needed information. – Heretic Monkey Aug 25 '21 at 18:52
  • @Jackdaw: The error is thrown at $(document).ready(function (). – user14322615 Aug 25 '21 at 19:13
  • @Camilo Terevinto: Sorry I copied the commented out code when I pasted into this. I've removed the comment syntax on the section Scripts. – user14322615 Aug 25 '21 at 19:13
  • My View has the Jquery reference because in my Layout I have defined the following the body tag. @RenderBody() @RenderSection("Scripts", required: false) – user14322615 Aug 25 '21 at 19:14
  • @user14322615: It might be you forgot to add the main `kendo` UI JS file. See [https://docs.telerik.com/aspnet-mvc/getting-started/first-steps](https://docs.telerik.com/aspnet-mvc/getting-started/first-steps) – Jackdaw Aug 25 '21 at 19:16
  • @CamiloTerevinto if you have code in the body with `` then have ` – freedomn-m Aug 25 '21 at 20:51
  • Comment above `@RenderBody – freedomn-m Aug 25 '21 at 20:54

0 Answers0