Last post Oct 10, 2019 02:43 AM by Fei Han - MSFT
Oct 09, 2019 04:11 PM|bank5|LINK
I'm building my first MVC project and loving it so far. My app will have an account creation view, a profile view and update profile view that all have the same fields. What's best practice, to use 1 ViewModel for all three Views or create separate separate
ViewModels for each view? I'm thinking 1 ViewModel for the 3 views but have read other recommendations for 1 to 1, so I wanted to ask here first.
Oct 09, 2019 05:35 PM|mgebhard|LINK
A forum cannot answer this question. View models are part of the UI. Craft the View model to support your UI. View models are also submitted from an HTML form to an action. You could end up with 6 different view models depending on your design.
Oct 09, 2019 09:01 PM|EnenDaveyBoy|LINK
it depends...(on a lot), choose the simplest (which is also subjective), and refactor it when you have more information
you could also have have 2 viewModels which inherate from the 3 keeping them all seperate and the same......
Oct 10, 2019 02:37 AM|bank5|LINK
Thanks for the replies. 1 to many seems to make the most sense in this case so I think I'll go with that for now.
Oct 10, 2019 02:43 AM|Fei Han - MSFT|LINK
My app will have an account creation view, a profile view and update profile view that all have the same fields. What's best practice, to use 1 ViewModel for all three Views or create separate separate ViewModels for each view?
As you mentioned, all of them would have same fields, so using 1 ViewModel to pass information/data is ok. If you do not have additional requirement for a specific view of these three views, you need not to create separate ViewModel class for each view.