0

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

enter image description here

Js files structure

enter image description here

asked May 22, 2015 at 18:18
17
  • In the script src, try replacing ../../ with ~/ Commented May 22, 2015 at 18:21
  • 1
    @Alex ohh dear, please expand that Scripts folder :) Commented May 22, 2015 at 18:51
  • 1
    @Alex well, seems like the script is there indeed. If you inspect your browser console when in your page, do you see a 404 when it tried to load the script? What if you try to browse to (root-address)/Scripts/Jquery-2.1.js? Commented May 22, 2015 at 19:01
  • 1
    What make you think its not loading? If your expecting to see an alert box to be displayed with "aa", then it has nothing to do with jquery - its because $("#btn") is undefined (and you really should use a tilde <script src="~/Scripts/jquery-2.1.js"></script> Commented May 23, 2015 at 6:35
  • 1
    Firstly jquery was always loading! You would have seen a $. is not defined in the browser console if it wasn't. The real problem was that you have the script at the top of the page and not inside document.ready. What happens is as the view is rendered, you have $("#btn").click(function () but at that point the element with id="btn" does not exist (its further down the page) so your actually attaching a .click() to something that does not exist (it's undefined) Commented May 23, 2015 at 7:17

4 Answers 4

1

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()

answered May 23, 2015 at 7:30
Sign up to request clarification or add additional context in comments.

Comments

0

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")
answered May 22, 2015 at 18:24

2 Comments

I don't think a bundle is necessary in this case. Also, OP seems to be a beginner, so a little more explanation would be interesting.
I am using mvc 3. u sure BundleConfig available in mvc3. I tried but couldn't add this file to app data folder
0

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.

answered May 22, 2015 at 18:37

Comments

0

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.

answered May 22, 2015 at 18:38

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.