I have MVC administration screens that I use to create/delete/edit and list data. Right now I have many viewModels (edit/delete etc) for each class of model. For example:
public class cityViewModel () {
public City City { get; set; }
}
public class buildingViewModel () {
public Building Building { get; set; }
}
What I would like to know is would it be better for me to combine these viewModels into one and use this common model. Something like:
public class adminViewModel () {
public City City { get; set; }
public Building Building { get; set; }
}
There are no official guidelines from creators of asp.net-mvc about when and how to use ViewModels. But from practical experience, single view model should be created for single view, 1:1. At the beginning, it may seem simpler to use single view model for everything, but as more data and fields appear on view model, you may find yourself explicitely excluding attributes while model-binding, complicated
ifs when rendering views, etc. So include in view models only fields needed for its view.Also, check out How we do MVC – View models by Jimmy Bogard