I am trying my first steps with Microsoft ASP.Net MVC. I set up a simple database for being able to test all facets like Controllers, Views, Model, ViewModel, etc.
The case behind is to have simple Projects and Resources. Each Project can have multiple Resources. Each assigned Resource to a Project can have a Role (string).
My database model looks like this:
Projects
Id <int>
Name <string>
CreatedAt <datetime>
CreatedBy <string>
Resource
Id <int>
Name <string>
CreatedAt <datetime>
CreatedBy <string>
ProjectResources
Id <int>
ProjectId <int>
ResourceId <int>
Role <string>
CreatedAt <datetime>
CreatedBy <string>
The first thing is to build the EntityClasses within the solution. I could have it like this:
public class Project
{
public int Id { get; set; }
public string Name { get; set; }
// IS THIS CORRECT? WHERE DO I HAVE TO PUT THE ROLE?
public List<Resource> Resources { get; set; }
public string CreatedBy { get; }
public datetime CreatedAt { get; }
}
public class Resource
{
public int Id { get; set; }
public string Name { get; set; }
public string CreatedBy { get; }
public datetime CreatedAt { get; }
}
This is the first part of the question. The second part is on ViewModels. What i have read is, that ViewModels should only contain those information which are rendered on a view. When i have 3 different views for 2 different purposes for example:
- Get list of projects
- Get single Project with assigned resources
- Save (edit/update & create new) project with assigned resources
So the question is: How could the ViewModel(s) look like? Do i need to have multiple ViewModels (from my understanding yes)? I have thought about it but dont know if this is ok (is there also any NamingConvention for ViewModels):
public class ListOfProjectsViewModel
{
List<Project> projects { get; set; }
}
But in this case where and how to put the joined Resources?
public class ProjectDetailViewModel
{
Project project { get; set; }
List<Resource> resources { get; set; }
}
In the class ProjectDetailViewModel i would have the list of resources for the dropdown but where to put the added/selected resources per project and the selected resources role?