3

I've read article "How we do MVC". In rule #3, said that "we only have one ViewModel type per View". For example, when we scaffolding in mvc, it's automatically create 5 Views (Create, Delete, Details, Edit, and Index). By that statement, is it mean that we have to create 5 ViewModels for that views? If my assumption is wrong, what is the best practice to use ViewModels?

Willy
  • 1,689
  • 7
  • 36
  • 79
  • That is correct. it is better practice to create a viewmodel for each view. this makes better development/maintainence and each view has its own viewmodel to deal with rather than bloating it for various views in 1 – Ahmed ilyas May 23 '14 at 09:58
  • @Ahmedilyas What about if sometimes or maybe some views have the same fields, do you create another ViewModel or reuse existing one? – Willy May 23 '14 at 10:11
  • It wasn't a Yes/No question – Worthy7 Jun 14 '16 at 03:47

1 Answers1

6

It is a common practice to create separate view model for different views.

However, from my understanding and practice, for details and edit view, sometime it is create, details and edit, the fields are almost the same.

In such case, I will reuse the same view model.

And there is a discussion over here, it might help you to understand it.

Community
  • 1
  • 1
Panda Zhang
  • 483
  • 3
  • 8