Wonko the Sane
Wonko the Sane

Reputation: 10813

ViewModel vs. Partial Views on Summary/Front Page

I have a design/best practice question relating to MVC3. I have a "front page" for the site that may contain a some summary information for non-related tables from my Model.

Is it better to create Partial Views dedicated to each Model, or to create a ViewModel to bind everything together?

The "pro" of Partial Views is that it truly separates concerns, with the "con" being that the partial views will probably not be reused elsewhere on the site.

The "pro" of ViewModels is that it allows a single strongly-typed object to be applied to a View, with the "con" being that the data is really unrelated and the binding artificial.

Upvotes: 2

Views: 461

Answers (1)

MikeSW
MikeSW

Reputation: 16348

For clarity and maintainability I'd choose this

 public class SummaryModel
 {
     public Patial1Model Partial1 {get;set;}
     public Patial2Model Partial2 {get;set;}
     //etc
 }

The main benefit is not reusability (but who knows?) but maintanability. With this model is very easy to modify data for a partial or to reuse it in other places.

summary information for non-related tables from my Model.

In a view, the ViewModel is the Model, In fact the app should not care about tables and other persistence details. The model of the app is the mainly the domain model, the pocos for EF or NH are persistence model. The views know about their model which is different from the app or the persistence model.

Upvotes: 2

Related Questions