Reputation: 21304
I began using ViewModels to create custom views that represent several underlying EF entities in MVC4. I created a folder named 'ViewModels' in my MVC project and have my classes within that folder. What I want to do is create a new view based on the strongly typed ViewModel class. However the wizzard displayed when creating a View in ASP.NET MVC with the "Create a strongly-typed view" option and subsequent "Model Class" dropdown selection does not contain my ViewModel class. The only classes listed are those from my Entity Framework model.
How do I create a View based on my created ViewModel class or rather get it to show in that list of Model classes?
Thanks!
Upvotes: 1
Views: 833
Reputation: 1512
You need to compile the MVC project before the new ViewModel class will show up in that dropdown.
Upvotes: 2
Reputation: 218722
Bu default, the Wizard will show all Class file
names except those Controller files (which ends with Controller.cs
)in the Model DropDown
Simply create a new normal View and then add this as the first line of the view file
@model YourViewModelClassName
Assuming YourViewModelClassName
is the name of your class you created for ViewModel.
ex : If you have a Customer View Model like this
public class CustomerViewModel
{
public string Name {set;get;}
}
So the view should be like this
@model CustomerViewModel
<p>Your view content can go here</p>
If your class ViewModel class resides in a a namespace, you need to specify the fully qualified class name
@model MyNameSpaceName.CustomerViewModel
Assuming MyNameSpaceName
is the name of your namespace where this class resides
Upvotes: 0