views:

148

answers:

2

When inside a View, within MVC, and you have your JavaScript, I tried to move it to a JavaScript file in the same directory, and it reports it can not file the javascript file

As an example I user NerdDinner and the DinnerForm.aspx, created a JavaScript file in the same directory as the DinnerForm.aspx called DinnerForm.js and referenced it in the aspx file, as below:

Here is the issue, I need to keep my JavaScript file near if not in the same directory in the project as DinnerForm.aspx, how do I go about doing this?

I don't want to place the full path, eg: "~\views\dinners\dinnerform.js" as this is just a work around, and will cause issues later

Any ideas?

+2  A: 

You can't have the JavaScript files in the Views folder as it does not actually exist to the browser. If you have a look at the Web.config file in the Views folder, you'll see that it is set to the HttpNotFoundHandler handler. That means it returns 404 for all requests to its folder.

Why not put them in the Scripts folder? You could always have sub folder under it.

E.g. /Scripts/Dinner/dinnerform.js

Manticore
Interesting info about HttpNotFoundHandler. Thanks!
Alexander Prokofyev
Yes I understand that you can do this, however it detaches the html from the JavaScript, make it hard to maintain at a later stage hence the reason for not doing this.At present the JavaScript is inside the html view, so moving it to yet another directory just adds to the complexity.
Coppermill
+1  A: 

JavaScript files by convention should be placed in project's Scripts folder and could be linked as

<script src="<%= Url.Content("~/Scripts/jquery-1.3.2.js") %>"
Alexander Prokofyev