11

I have a very odd problem. Whenever I try to use the "Add View" dialog in ASP.NET MVC2 and then try to "Create a strongly-typed view" by selecting a "View data class" from the drop down of available classes none of the classes ("models") in my MVC project are showing up.

The very odd part is all of the assemblies that my MVC project is referencing, even other projects in the solution, their classes are showing up. I have tried cleaning, rebuilding, cleaning the obj folder but every single time for some reason none of the classes in my actual MVC assembly are showing up. It was working fine before but now it doesn't anymore and I can't really think of anything that has changed.

Anyone experienced this issue before? Thanks for the help!

Image of example:

http://imgur.com/47itE.png

John Culviner
  • 22,235
  • 6
  • 55
  • 51
  • Having the exact same issue with this dropdown, normally its fixed by simply rebuilding the file, but its not working now. Its picking up all of the classes from my referenced dll's its just not picking up the classes that are in my mvc web project, did you manage to find some sort of fix? – spaceman Feb 21 '11 at 09:37

5 Answers5

13

Its Because you have not build your application, First build your application and then make a view then it will show .

Anup Ghanshala
  • 155
  • 1
  • 3
  • 2
    This is definitely the first thing to try since VS uses reflection on compiled code to discover possible ViewModel types. I did mention in my question that I tried cleaning and rebuilding and still experienced the issue however. – John Culviner Oct 08 '12 at 16:16
2

Figured this out, here is the solution:

The MVC Project was referencing a bunch of service contract assemblies that where referencing a "CommonServiceContractAssembly.dll". The MVC Project was also referencing "CommonServiceContractAssembly.dll". The MVC Project and the service contract assemblies were all built referencing slightly different versions of "CommonServiceContractAssembly.dll"

When Visual Studio 2010 was reflecting on all referenced assemblies to "Create a strongly-typed view" of I believe it wasn't sure how to handle the slightly different version of "CommonServiceContractAssembly.dll" being referenced so it didn't display the reflected "strongly typed model" possibilities for any assemblies dependent on "CommonServiceContractAssembly.dll".

The fix is to actually force Visual Studio 2010's app domain to use the correct version of "CommonServiceContractAssembly.dll" when Visual Studio 2010 starts up. This was accomplished with a Post Build Event like the following:

REM This is required for T4 generation from models to work properly copy "$(TargetDir)"CommonServiceContractAssembly.dll" "$(DevEnvDir)PublicAssemblies\" /Y

So I copy "CommonServiceContractAssembly.dll" that is referenced "everywhere" into where Visual Studio will load it. After I did this everything worked properly.

Another option would be to always ensure that all assemblies that share a common dependency are always compiled with the same version of that dependency.

John Culviner
  • 22,235
  • 6
  • 55
  • 51
  • i'm having the exact same problem that you're describing here. i've checked references and updated everything to use the same version for all dependencies, but the problem persists. how did you figure out your solution btw? – spaceman Apr 06 '11 at 08:07
  • Hmm, I'm sorry to hear that solution isn't working for you. Take a look in reflector and verify the above too, you never know. How I managed my solution was thinking about how Visual Studio must be loading my assemblies to reflect on them to determine "ViewModel Candidates". Based on previous experience I know that .NET (and VS since it runs on .NET) can get "upset" if all referenced versions don't match up. I bet VS2010 throws an exception somewhere internally that is handled and just decides to quit on trying to resolve the affected assembly.. Keep us posted! – John Culviner May 26 '11 at 06:50
  • This answer pointed me to the right direction. My project was referencing for example `Assembly X 1.0.1` while one of the other references was using `Assembly X 1.0.0`. Updating the reference in that class library to the newer version did the trick. – Marthijn Jun 25 '12 at 14:06
  • Awesome, I'm glad this could help! I wonder if VS logs out an exception somewhere about it (I was never able to find one). The UI just eats the problem which is even worse. VS2012 maybe? – John Culviner Jun 25 '12 at 14:56
1

Please make sure that you are marking them as "Public", and compile it once before opening the AddView Dialog box.

Ajay Bhosale
  • 1,872
  • 24
  • 35
-1

Before Adding View just Build the solution and then add view it will work. if it will not work then you can again concern with me i will give you another solution

-1

Some types from your project are filtered out by that dialog (for example all types ending in 'Controller', or all types in the System or Microsoft namespace). It's possible that your project does not have any types that would pass through the dialog's filters.

marcind
  • 52,944
  • 13
  • 125
  • 111
  • Thanks for the response! This isn't the problem. I have say "Person.cs" in the models folder of my MVC project. For some reason Person.cs isn't showing up at all in the drop down even though it did before (or classes of a similar nature)! Quite odd I know. It wouldn't be a big deal, but my company will soon be using the T4 capabilities of this tool to create our own templates so I have to figure out whats going on in order for us to be efficient. – John Culviner Sep 23 '10 at 05:41