Reputation: 9234
Would it be considered bad practice to have a viewmodel that has a property of another view model?...as in:
public class PersonViewModel
{
public PersonJobViewModel Peron { get; set;}
//other properties here...
}
EDIT
A little more about my particular situation:
I have a view model that currently contains 2 domain classes. I pass this viewmodel to a view that loads 2 partials views(one for each domain class in the viewmodel)
So with this I end up passing pure domain models directly into the partial views.
My thinking is that I can make a view model for each domain model that go to the partials...and then wrap those 2 in another viewmodel that gets passed to my parent...
or is there a better way to accomplish this?
Upvotes: 9
Views: 1105
Reputation: 11285
It is definitely okay. View Models should, in concept, mimic your domain model with relevant information for a given view(s).
Just remember that once "best practices" become counter-intuitive and counter-productive they may no longer be the best practice for you in that given scenario. Best practices are guidelines, not strictly adhered to requirements.
Edit: Changed my initial comment because I'm not sure I was clear enough before in saying that it was okay to do.
Edit2: Also ask yourself whether you even need view models. If they literally mimic your domain models, what's the point? Just use your Domain Model (unless you have other dependencies that would make this ugly).
Upvotes: 3
Reputation: 2691
No, you may have partner model vs some fields like:
public class Parner
{
int age {get; set;}
//etc
}
public class ParnerList
{
public List<Partner> ListOfPartner {get; set;}
public int PageNumber {get; set;}
public int PageCount {get; set;}
//etc
}
It's easy to use in View for show list vs paging
Upvotes: -2
Reputation: 8098
I don't believe that I would consider it bad practice to aggregate one ViewModel within another. I can see an advantage, like being able to render a partial view or use an EditorFor
of the aggregated view model.
Upvotes: 6
Reputation: 1038720
No, it's not bad at all. It's perfectly fine code. It allows you to reuse portions of view models between different views.
Upvotes: 13