15

I am using Visual Studio 2010 / ASP.net MVC 3 with the Razor View Engine. I created a new Project with the Internet Application template. What do I need to do to get Intellisense working?

Dismissile
  • 32,564
  • 38
  • 174
  • 263

4 Answers4

31

If that's jQuery specific as the title specifies, trying adding line to the tag in the Views/Shared/_layout.cshtml (or .vbhtml) file:

@if (false) { <script src="../../Scripts/jquery-1.4.4-vsdoc.js" type="text/javascript"></script> }

This will reference the intellisense file to VisualStudio and still will not reference it in runtime.

Just remember, point to the vsdoc file relatively to the file you put this code in. Any code like ~/Url.Content() or any other runtime code will not be visible to VS for intellisense.
That's exactly why if (false) hides the script reference from runtime (the if block isn't executed), but doesn't hide it from VS intellisense (and provide another reference using Url.Content() or so to the .min.js file).

Meligy
  • 35,654
  • 11
  • 85
  • 109
  • That didn't work either unfortunately. Might need to reinstall VS2010 or something. – Dismissile Mar 03 '11 at 02:43
  • Do you have Resharper installed? If so, which version? and Does changing intellisene option in R# settings to Visual Studio change anything? – Meligy Mar 03 '11 at 02:46
  • You can use the same trick in *.js files. Drag and drop from the solution explorer. I didn't realize this at first, hope it helps. – Daniel Harvey Feb 29 '12 at 01:29
  • @MohamedMeligy: That works for me. Along with the comment about not using `@Url.Content(...)` on Greg's answer. What I do not understand is, what is the meaning of `if(false){...}` That seems to be pointless but it works!! Can you explain? – Mohayemin Apr 25 '12 at 03:29
  • The `if(false) {...}` bit is to prevent the code from executing in runtime, so that the large heavily commented -vsdoc.js is not referenced in runtime (you are expected to have a separate reference to the min.js file as well to be executed and referenced as you do normally). Using `Url.Content()`/`~` or any code that's evaluated in runtime will prevent VS from detecting the -vsdoc.js file for intellisense. I've added these notes to the answer itself. Thanks for bringing it up. – Meligy Apr 25 '12 at 07:17
  • Additionally to add the `intellisense.js` file I needed to add the jQuery file too. – Vitor Canova Mar 09 '16 at 18:27
9

From http://blog.meidianto.com/2010/05/13/vs2010-tips-7-how-to-make-jquery-intellisense-work-for-external-javascript-file/

Drag the jquery file into the js file you want intellisense on like this: enter image description here

Then it will work like this: enter image description here

Bryan Legend
  • 6,790
  • 1
  • 59
  • 60
  • 1
    The question is about Razor files (.cshtml, .vbthml) not JavaScript files. The `` syntax works when you need automcomplete from one JavaScript file in another JavaScript file (.js). – Meligy Jul 20 '13 at 09:57
  • I am also facing same problem, Tried your drag and drop referencing. Now it is working, But in this way I have to drag and drop jquery*.js file in all my views. Is there a way so that I will reference in Layout view and it will automatically come in all views – Amit Mishra Jan 03 '14 at 10:30
4

I found good explanation which worked for me at: http://theycallmemrjames.blogspot.com/2011/03/jquery-intellisense-with-aspnet-mvc-and.html

These lines:

 @if (false) 
{ 
    <script src="../../Scripts/jquery-1.4.4.min.js" type="text/javascript"></script> 
    <script src="../../Scripts/jquery-ui.min.js" type="text/javascript"></script> 
}

should be added to every view (where intellisense is needed). This will enable intellisense but will not add the second reference at runtime.

Greg
  • 41
  • 1
  • 5
    I have found that intellisense does not work when you use Url.Content("~/Scripts/jquery-1.4.4.min.js") in your script references. Apparently it can't determine the path until you run. YOu also seem to need to do this in every single page you want intellisense in, not just adding it to the layout/master. – Dismissile Mar 23 '11 at 22:25
  • 6
    Wow, so basically the intellisense is completely useless unless you clutter EVERY view with useless if blocks for EACH javascript reference you want intellisense for. Really disappointed in Visual Studio right now. – John B Nov 17 '11 at 15:31
  • @John Bubriski see my ans below. – MemeDeveloper Mar 22 '13 at 02:39
2

Seems for me in Vs2012 with a _layout.cshtml that the solution from @Mohamed Meligy doesn't help in views using that layout file. Maybe I'm missing something?

However - the solution here which seems to be recommended approach seems to work a treat and now I have intellisense for all references to all files I add references to in _references.js and don't need the runtime false trick bit, and can keep the views totally clean of references.

Community
  • 1
  • 1
MemeDeveloper
  • 6,457
  • 2
  • 42
  • 58