This question has been asked a few times in SO but nothing helped so far in my case.
To an existing MVC project I added a model generated from a database (using database first). Now I would like to add a new API controller. However, neither my new model classes nor my data context class show up in "Add Controller" dialog:
made sure I recompiled my dll
restarted VS (2010)
deleted AssemblyInfo.cs (as suggested in another SO post)
checked with ILSpy to make sure models are in and have an Id
My workaround is to put my models in another dll. However, I think that shouldn't be necessary...
Could it be as simple as a namespace or reference issue.
If they are in different projects then the controller project will eed to reference the respository project.
Can you get to the models by using the full namespace path - you made just need to add a suitable using in the class; typing CTRL+. will give you suggestions.
Hope that helps a little.
Related
I originally had my EF class in a project added to the solution with my MVC app project referencing that project directly in the solution. I added a bunch of scaffolded items with no issue. I recently moved my EF project to a separate solution and am pulling the dependency with a NuGet package in my MVC app, and now when I go to add a new scaffolded item, the EF class nor the EF models are available to be added.
The existing scaffolded items still function fine and has no issues. What am I missing here?
The scaffolding in MVC is very finicky. In particular, it only works on entity classes that belong to the context and, importantly for you, the context must reside in the same project as where you are running the scaffold. In other words, unless you're building a very basic MVC project, the scaffolding will very quickly become obsolete.
The good news is you don't need it. Unless, you're brand spanking new to MVC development, altering the scaffolded controllers, views, etc. will be far more time-consuming than simply creating everything from scratch. There's nothing magical about any of this. A controller is just a class that inherits from Controller. A view is just a text file with a cshtml extension. Everything else is application-specific anyways, meaning that you would have to touch everything that was scaffolded anyways.
I'm currently working on an ASP.NET project which I've not done before, and I need to use Entity Framework to access a database. I have a database already and following along with this tutorial: https://msdn.microsoft.com/en-gb/data/jj200620
I've managed to get Entity Framework to create the classes for the tables in my database but it has created them all in my Views/Home directory like so:
But from my understanding of MVC these should go in the Models directory? Is it safe to just cut and paste these generated files into Models or am I going to run into problems in the future because it's going to still be trying to use View/Home?
It should be safe to move them to Models. The models are compiled, not deployed as website content, so ASP.NET does not care about their exact location.
You will probably want to update the namespaces of the models, though. I'm guessing that the models were created with a namespace like YourNamespace.Views.Home, and you will want to change it to YourNamespace.Models.
Also make sure to update the namespaces to any references to your models (if you have any). You will get compile errors for any references that you missed EXCEPT for any references in cshtml files.
It would be a lot easier to just delete everything created by EF, and add your ADO.NET Entity Data Model (.edmx file) again into the right folder.
On step 3 of the EF guide in your question, when you add the ADO.NET Entity Data Model, make sure you add it into the Models folder. (Right click on the Models folder, then Add New Item...)
I am currently stuck trying to add my own class file into ASP.NET MVC project, so it could
be referenced by my controller. But Visual studio 2010 always complain about "The type or name 'Products' could not be found (are your missing a using directive or assembly reference?)
I am not sure where to put my class file and tried add the class to Models or Controllers directory, and it wouldn't work either. My the class is under the right namespace, and I did reference the Models namespace in my Controller. It could be something obvious, but I couldn't get it working:(
Make sure that the "Build Action" property of the file is set to "Compile" in Visual Studio. Perhaps when you initially added the file, you added it to a location or added it as a file type that did not automatically set the build action properly.
The general rule of thumb is that business objects go it the Models folder you're correct. If it isn't working you don't have the project configured correctly. In the controller where you wanted to reference the Products object, did you add a reference to Models.Products?
Are there any other errors in listed? Sometimes multiple errors in a solution can combine to create something like this.
Ensure the namespace of your class is correct.
Based on a comment to a different answer, it appears you're trying to add a utility or helper class. I suggest you put it in a Helpers folder under the root of the project.
So first add the Helpers folder under the root of the project. You should have the following structure when done (there may be other folders as well):
[project]
- Content
- Helpers
- Controllers
- Views
Now, add a new class to the Helpers folder. Call this class ProductsHelper (in the Add New Item dialog make sure to put ProductsHelper.cs, as it's asking for the file name).
Now have a look in the new ProductsHelper.cs file. Copy the namespace found in said file, to be used in the controller.
Now, add a using directive at the top of your controller that looks like the following:
using [copied namespace];
Sometimes projects are set up to use default namespaces that don't match the project name, in which case the namespace in your newly-added class file might be different than what you thought you should add as the using directive. If you're wondering what the default namespace for your project is, you can see it in the project's properties, on the Application tab, in the "Default namespace" textbox (you can change it here as well).
I had the same problem adding some utility class .cs files to my new MVC project.
I was working mostly on VS 2010 Website projects which compile differently from
Web Application Projects ( WAP ) like the MVC 3 Project.
Just to clarify Jacob's answer, In the VS Solution Explorer: right click on the class .cs file and select the Property Dialog and then set the property: Build Action to Compile.
This will force the code to be compiled when you Build the project.
HTH, LA Guy
is there any particular directory that i should put my code into in an asp.net mvc project
i have some extentions to the HtmlHelper class. Right now i have it sitting in the Content folder. is this correct? is there a better soluiton?
I usually create a separate project (or projects) for my own code, including my data layer, as class libraries. I then reference the libraries in my MVC web site.
you can put code wherever you want, but typically you want things organised. heres how i do it:
2 assemblies
MyProject.Domain
this contains all my domain code; business logic and entities
MyProject.Web
this contains controller code, views and assets like css/images
Your HtmlHelpers belong in the .Web project because they are mvc related (nothing to do with the domain). You probably want a new folder called Helpers or Extentions. Its really up to you, the key point is to decide where something belongs and to namespace it accordingly
I agree with what everyone else said, here's how one of my solutions would look like:
1- MyProject.WebUI
2- MyProject.DomainModel
3- MyProject.Test
4- MyProject.Extensions
This extensions project is new to me (actually since I knew about extension methods). It usually concludes sub-folders describing what the extension methods are used for, for your particular case, the folder name would be HtmlHelpers. I then reference this project (or its output library when using elsewhere). HTH
If you are going to re-use the same HTMLHelper extensions in different ASP.NET MVC projects, I'd suggest putting them in a class library which is completely seperate from your project.
Trying to create a MVC User Control in the Release Candidate and I can't see to make one with a codebehind file. The same is true for MVC View pages.
Creating Views in the Beta would produce codebehinds...am I missing something?
Code behind kind of defeats the purpose of the MVC Framework. Functionality should be kept separate from the view, the MVC team felt that code behind pages went against this ideology and therefore removed them.
Your can create a custom helper method to create your control. Also I'm not sure if MVC has view components (Monorail/Castle) but that could be an option as well.
From ScottGu's Blog post:
*Views without Code-Behind Files
Based on feedback we’ve changed view-templates to not have a code-behind file by default. This change helps reinforce the purpose of views in a MVC application (which are intended to be purely about rendering and to not contain any non-rendering related code), and for most people eliminates unused files in the project.
The RC build now adds C# and VB syntax support for inheriting view templates from base classes that use generics. For example, below we are using this with the Edit.aspx view template – whose “inherits” attribute derives from the ViewPage type:
One nice benefit of not using a code-behind file is that you'll now get immediate intellisense within view template files when you add them to the project. With previous builds you had to do a build/compile immediately after creating a view in order to get code intellisense within it. The RC makes the workflow of adding and immediately editing a view compile-free and much more seamless.
Important: If you are upgrading a ASP.NET MVC project that was created with an earlier build make sure to follow the steps in the release notes – the web.config file under the \Views directory needs to be updated with some settings in order for the above generics based syntax to work.*
I answered this question here:
How to add a Code-behind page to a Partial View
Seems this wasn't particularly tricky, and is quite do-able
This answer worked for a Partial 'ViewUserControl' but the same should apply
Ok.
First: Add a Class file with the convention of .cs (i.e. view.ascx.cs)
Second: Add "using System.Web.Mvc;" to the class
Third: Change the Class to Inherit from "ViewUserControl<>"
Fourth: Add the following to the View's header:
CodeBehind="View.ascx.cs" Inherits="Project.Views.Shared.View"
Fifthly: Copy the files out of the solution and drag back in to reassociate the two together
Note: For this to work with a Normal MVC View you just need to inherit the class from "ViewPage"
The whole idea for ASP.Net-mvc was to get rid of the codebehind files...thats why asp web controls didnt matter that most didn't work.But with the changes of getting rid of the code behind comes with a different programming style..The idea is codebehind files are EVIL:
http://stevesmithblog.com/blog/codebehind-files-in-asp-net-mvc-are-evil/
the whole idea is to make sure people remember they are using asp.Net-mvc and not asp.et web pages. take alook at this link ,it explains it a little better:
http://blog.lozanotek.com/archive/2008/10/20/Visual_Studio_Templates_for_MVC_Views_without_Codebehind_Files.aspx
I think this tutorial is what you are asking.. but not really sure what you want..