简体   繁体   中英

JS files are not linking to asp.net MVC view

I am creating an asp.net MVC application.

I have a view named "receive" and it placed in Views/Main folder.

I am trying to add js file.

In intellisense, I am getting access to js file functions.

But when I run the application, the js file is not linking.

My view is as follow

 @model dSite.Models.MModels
 <script type="text/javascript" src="../../Scripts/Jquery-2.1.js">        </script>
 <script>         
   $("#btn").click(function () 
       { alert("aa"); 
    });

</script>
@{
     ViewBag.Title = "receive";
 }
<h2>
receive</h2>
<input type="text" id="txtname" placeholder="enter name" />
<input type="button" id="btn" value="click here" /> 

And my Scripts folder is at the root.

How can I link my js file to my view? I have tried the above but it's not working

following is the file structure

在此处输入图片说明

Js files structure

在此处输入图片说明

Firstly jquery is being loaded! You would have seen a $ is not defined error in the browser console if it wasn't.

The real problem was that you have the following script at the top of the page but not inside document.ready.

<script>         
    $("#btn").click(function () { 
        alert("aa"); 
    });
</script>

What happens is as the view is rendered, the first line of the script is trying to attach a .click() event to the element with id="btn" , but at this point that element does not exist in the DOM (its further down the page) so the you actually attaching the event to an undefined element (one that does not exist).

Unless you place your scripts immediately before the closing </body> tag, you should always wrap your scripts in $( document ).ready()

Use the built in MVC bundling on your css and js files.

In your App_Data/BundleConfig.cs do this:

 bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
                "~/Scripts/jquery-{version}.js"));

Replace your script tag in your view with this:

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

Update your script tag to this:

<script type="text/javascript" src="@Url.Content("~/Scripts/Jquery-2.1.js")"></script>

I believe in MVC 4+ (Razor 2) you can simplify this to

<script type="text/javascript" src="~/Scripts/Jquery-2.1.js"></script>

The tilde gets translated into your web application's root path so that you can specify paths relative to that.

When a view is created and rendered all scripts are published in a folder Scripts at the root of your website so when you reference a script you must reference this folder. The scr atribute of the script references a absolute or relative url. Those can be referenced directly with a string or you can use the framework to generate one for you .

Check Absolute urls, relative urls, and...? to learn more about absolute and relative urls.

Some examples of the above using a string to reference the script's address

<script type="text/javascript" src="/Scripts/Jquery-2.1.js"></script>
<script type="text/javascript" src="~/Scripts/Jquery-2.1.js"></script>
<script type="text/javascript" src="http://mysite/Scripts/Jquery-2.1.js"></script>

You can also use url helpers like @Url.Content("~/Scripts/Jquery-2.1.js") and you will get an url generated by the framework acording to your defined routes.

<script type="text/javascript" src='@Url.Content("~/Scripts/Jquery-2.1.js")'></script>

I personaly recommend you that you use the MVC bundling and minification feature that will come handy in a production enviroment. This is located inside the BundleConfig.cs and you will have to specify which files you want to be generated.

bundles.Add(new ScriptBundle("~/bundles/myscript").Include("~/Scripts/myscript.js"));

Where myscript points to the javascript file you want to include and later reference it using

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

This will generate a script tag like the one you are using.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM