Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use jquery in ASP.​NET Core

I created a ASP.NET core template and wrote a jquery script. When I look at the page I see that jquery is loaded into the page, but the script doesn’t run. I looked at the ASP.NET docs page and my layout.cshtml looks the same, so are there extra steps I must take to get jquery working? enter image description here Home page

@{
    ViewData["Title"] = "Home Page";
}
<!-- Page Content-->
<div class="container">
    <div class="row">
        <form method="post" enctype="multipart/form-data">
            <input type="file" id="files" name="files" multiple />
            <input type="button" id="upload" value="Upload Selected Files" />
        </form>
    </div>
</div>
<script>
    $(document).ready(function () {
        alert("Test"); 
    });
</script>

Solution

@section scripts
{
    <script>
        $(document).ready(function() {
            alert("Test");
        });
    </script>
}
like image 583
AJ_ Avatar asked Jun 20 '16 13:06

AJ_


People also ask

How can add jQuery file in ASP NET MVC?

After the ASP.NET MVC Framework is installed a new ASP.NET MVC Web Application project should be created. Next, download jQuery, get the Packed or Minified version and place it into the Content folder of the the new web application project. Add a reference to the jQuery file put in the Content folder.


2 Answers

I suspect your jquery is loaded after the rest of the page content.

This means that you cannot reference jquery objects as the library has not been initialised yet.

Move the page script after jquery has loaded.

<script src="~/lib/jquery/dist/jquery.js"></script>
<script>
  $(document).ready(function () {
    alert("Test"); 
  });
</script>

For efficiency I recommend you do this in one of two ways:


OPTION 1

Use a master script file that loads after jquery.

<script src="~/lib/jquery/dist/jquery.js"></script>
<script src="~/js/master.js"></script>

OPTION 2

Use a placeholder template that will always load after jquery but can be initialised on individual pages.

Master _Layout Page

<script src="~/lib/jquery/dist/jquery.js""></script>
@RenderSection("Scripts", required: false)

Content Page

@section Scripts {
  <script>
    $(function () {
      alert("Test"); 
    });
  </script>
}
like image 145
DreamTeK Avatar answered Oct 21 '22 02:10

DreamTeK


FOR CLARIFICATION


If you are referencing jQuery in your _Layout page.. double check to ensure that that reference is at the TOP of your _Layout page because if it is at the bottom, then every other page you have that use the _Layout and has jQuery, it will give you errors such as:

$ is undefined

because you are trying to use jQuery before it is ever defined!

Also, if your are referencing jQuery in your _Layout page then you do not need to reference it again in your pages that use your _Layout style


Make sure that you are loading the reference to jQuery before you start using <scripts>.

Your cshtml should work if you put the reference above your script as so:

<script src="~/Scripts/jquery-1.10.2.js"></script> // or whatever version you are using

// you do not need to use this reference if you are already referencing jQuery in your Layout page

<script>
    $(document).ready(function () {
        alert("Test"); 
    });
</script>
like image 14
Grizzly Avatar answered Oct 21 '22 02:10

Grizzly