Visual Studio "Add Controller" list of model classes - asp.net-mvc

The "Add Controller" dialog on an MVC project provides a dropdown list of model classes to choose from. These are usually the model classes in the project, and that's what I used to see. But now I'm getting this:
Any idea what's wrong and how to change it back to what I believe is the normal way?

I often run into a problem with that dialog box "forgetting" classes and showing odd choices. Closing and restarting VS has always cleared the problem up for me.

It seems there's no solution to this problem. That dialog shows the classes from all the classes in the solution, and that includes any 3rd party classes that were imported (using something like NuGet). Which kind of makes sense. So it's a feature, not a bug.
The workaround to sifting through that whole list is to take advantage of the autocomplete functionality built into that dialog, which makes it quite easy to find the required full class name.

Related

Refactoring features for ASP.NET Core on VS 2017 are not working very well for me… Am I doing something wrong?

I am in the process of learning ASP.NET Core MVC, while experimenting, I noticed that when I try to refactor the code using the built in refactoring tools provided by Visual Studio they don't work very well. For example:
Renaming a class via the solution explorer pane usually fails. When it succeeds it will fail to rename the class on the view declaration (eg: #model ClassNameIsNotRenamed);
If I rename a controller or action, the helper tags such as asp-controller="NotRenamed" and asp-action="NotRenamed" do not get updated.
etc.
I don't want to get too detailed on what works and what does not work, the point that I am trying to make is that VS 2017 does not appear to do a good job when it comes to refactoring.
So my question is... Am I asking to much from VS 2017? Is there something that I can do to make refactoring work better?
Thanks.
Doing something like a rename requires that your project can build successfully. If you attempt rename something like a class, and the project either hasn't been built or failed the build, the rename will fail as well. This is fairly logical, as doing a rename refactor requires tracking references, which it can't do without the IL.
As far as your Razor views and controller/action references go, you're dealing with strings and/or code that is not compiled. As a result, it doesn't participate in code-based refactoring generally. Certain things like renames give you option to search strings as well, which you can do to catch more places where things should be renamed, such as your controller/action references. However, that can also cause unwanted side effects if you happen to be using the same string somewhere else, in a different context (which is why string replacement is not done by default).

Code Cleanup - asp.net mvc

This might be a weird question to ask.. but nevertheless this is my question.
I have a huge Asp.net MVC project with lots of model classes. As the requirement grew, we kept on adding some properties and deleting some. Eventually I ended up having properties which are not referenced anywhere in the project.
Is there any quick way to find out these orphan properties.?
ReSharper can do this. Right-click on your solution and selection "Find Code Issues".
One of the results is "Unused Symbols". This will show you classes, methods, properties etc., that aren't used.
I would use resharper to assist you with this task. In case you don't want to spend the $349 you can use it for 30days for free. Resharper allows you to analyse the whole solution and highlights obsolete/unused code.

Add View Model class dropdown not showing my class

I’ve created a new C# asp.net mvc 4.0 (beta) Internet Application and I’m having a somewhat odd behavior in the Add View dialog in Visual Studio 2010 (sp1).
When I decide to create a strongly-typed View, inside the Add View dialog, the Model class dropdown does not show my ViewModel class.
For the record, yes I did compile the application before triggering the Add View dialog.
I believe I have pinpointed the issue but do not know how to fix it.
Below are the repro-steps if anyone cares to reproduce the behavior:
In Visual Studio 2010 (sp1), create a new C# asp.net MVC 4.0
(beta) Internet Application.
Inside the Models folder, create a new simple class called
“GazouViewModel.cs”
Build the application, open the HomeController, right-click the
Index() ActionResult and select Add View…
In the Add View dialog, check the “strongly-typed view” checkbox and
in the Model class: dropdown, you’ll notice your “GazouViewModel”
In Solution Explorer, right-click your project and select “Manage
NuGet Packages”
Search, find and install the “Bootstrapper.Autofac” package.
Rebuild the application and verify if your “GazouViewModel” still
displays inside the Add View dropdown’s dialog.
The “GazouViewModel” should still be displaying in the dropdown.
In Solution Explorer, right-click your project and select “Manage
NuGet Packages”
Search, find and install the “Autofac ASP.NET MVC4 (Beta)
Integration” package.
Rebuild the application and verify if your “GazouViewModel” still
displays inside the Add View dropdown’s dialog.
The “GazouViewModel” should still be displaying in the dropdown.
In Solution Explorer, right-click your project and ADD a new class
(Class1.cs) anywhere it doesn’t matter.
Make that Class1 implement IAutofacRegistration and implement the
interface.
Rebuild the application and verify if your “GazouViewModel” still
displays inside the Add View dropdown’s dialog.
The “GazouViewModel” is no longer showing inside the Model class
dropdown.
It appears that as soon as I create a class that implements the IAutofacRegistration interface and build the application, I’m no longer capable of viewing my ViewModels in that dropdown.
Can anyone confirm if they are getting the same results as me?
And more importantly, how can one fix this?
IMPORTANT: I have tried the exact same steps with an mvc 3.0 application and the exact same behavior occurs…so this is not relevant to the BETA version.
Sincerely
Vince
If you get error message: "Running transformation: The input file appears to be using a schema version not supported by this template. This may lead to compile errors. Please use 'Add New Generated Item' to add an updated template".
You are used EF 5.x. You have to add metadata for EF 5.x to your T4 template. In file header call DefineMetadata()
<## template language="C#" debug="false" hostspecific="true"#>
<## import namespace="System.Text.RegularExpressions" #>
<## include file="EF.Utility.CS.ttinclude"#><## output extension=".cs"#><#
DefineMetadata();
at the bottom of your template create method
private void DefineMetadata()
{
TemplateMetadata[MetadataConstants.TT_TEMPLATE_NAME] = "CSharpDbContext.Types";
TemplateMetadata[MetadataConstants.TT_TEMPLATE_VERSION] = "5.0";
TemplateMetadata[MetadataConstants.TT_MINIMUM_ENTITY_FRAMEWORK_VERSION] = "5.0";
}
I solved this issue by beeing mad in like 2 hours and then deleting the Assembly.cs from the Properties-folder in the solution. After I made a rebuild of the solution, the "Add-view" model-dropdown suddently could find all my ViewModels, strange enough.
I guess the assembly version defined in Assembly.cs somehow got mixed up with the actual version used in the project. Anyhow, it works now for my case.
What worked for me was to simply build the solution after creating my model. Then I was able to find my model in the dropdownlist.
I've noticed this issue as well (using MVC 4 Beta on Visual Studio 2010). Re-starting Visual Studio solved the problem for me.
Just another solution to add to the pile as none of the other ones worked for me. This is another example of Visual Studio being terrible at handling long file paths and namespaces.
In my case the namespace was too long to display in the "Model class" dropdown. Shortening the namespace (a ridiculous pain in of itself) made the drop down work again!
UPDATE: As of this writing, I can no longer reproduce the error. Perhaps the NuGet packages I use have been updated to a newer version and incidentally fixed the error.
I will mark this post as closed.
Thanks for everyone’s input on this!
Vince
Just Clean and build your solution. It worked for me.
Using VS 2010 and MVC 3.0, at one point the Add View Model class dropdown was not showing any classes for me. Ivan's suggestion of restarting VS appeared to work for me.
I tried deleting AssemblyInfo.cs, did a Clean, and a Rebuild, but those didn't resolve the issue. When I restarted VS, the model classes were back in the dropdown.
I realize this is a bit out of date. I had a similar situation where my classes wouldn't load into the View Template wizard. I'm using VS 2012 with MVC3. Everyone else in the team was OK, just luckily old me had issues. After much research and trying lots of things the only thing that has consistently fixed it is running VS in safemode (from the command prompt: "C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\devenv.exe" /safemode). One of my extensions must be causing the issue and by running in Safe Mode those aren't loaded.

Create reusable 'base code' using MVC2-RC?

I am trying to write a simple library for MVC2 projects that takes care of user login, e-mail validation, password recovery, etc. Since some of these steps involve user interaction, I need to have Views and Controllers in that project.
In MVC2 Preview 2, one could do this by abusing areas (at least, it seems) since they were implemented in different projects. However, in RC a new area will merely create a subfolder. Since I want to share the base code between applications, that is not an option.
I tried to brutally hack the AfterBuild steps into my project files, but that doesn't 'feel right' (I wouldn't sleep another night :)
Could you share some thoughts on how to do this?
This question might be a duplicate of Areas over multiple projects - Views not found in child projects
The build task to copy the views have been moved to the Mvc.Futures project. Other options to solve the problem include defining the views in the application, even when they are being referred to by controllers in the library. Also, the MvcContrib project offers portable areas.
A thorough explanation by Phil Haack can be found at http://forums.asp.net/p/1494640/3540105.aspx

How can one use the "Add view" dialog in Visual Studio efficiently when working with ASP.NET MVC?

Does anyone else think that the add view dialog in VS is useless or is it just me?
Why is there no search/filtering in there? I can not even paste the type name (without namespace) there to speed up the view creation.
Plus, there are a lot of irrelevant classes there, including classes from all referenced libraries.
Am I missing something or Microsoft never did any usability testing with that??
Is it possible to filter out types/assemblies in the dialog and/or better navigation compared to manually scrolling the combobox with all the types listed? Is it possible to include only types from a particular directory/assembly/namespace? (the ViewModels namespace for example)
EDIT: As it seems from the answers that there is no usable workaround for this, I have filed a suggestion at Connect: https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=507784
Please vote for the suggestion if you find this is an important issue.
There is search/filter, but it's a "starts with" search. Start typing and the list will be filtered.
You can paste the type name, but you do need the namespace.
No, I don't think you can remove assemblies.
Could it be improved? Sure. Some of your suggestions are good, and you should consider filing Connect reports for them. Is it useless, IMHO, no.
I tried the dialog in VS2010. As of beta 2, it behaves the same as VS2008, AFAICS. So get your suggestions in soon!
I mainly agree with you. While it's not competely useless, tha fact that all the classes from the referenced libraries show up and that it doesn't have a search box make it slow to add a new view. Maybe you could file a wishlist bug report on the ASP.NET MVC site...
As it seems from that there is no usable workaround for this, I have filed a suggestion at Connect.
In case anyone else feels that this dialog should be improved, please vote for the suggestion in Connect.
Resharper 5.0 implements this dialog very efficiently, solving all the described problems.

Resources