locked
ViewModel -> View : 1 -> 1 or 1 -> Many RRS feed

  • Question

  • User-29703693 posted

    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.

    Wednesday, October 9, 2019 4:11 PM

Answers

  • User1034446946 posted

    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......

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 9, 2019 9:01 PM

All replies

  • User475983607 posted

    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.

    Wednesday, October 9, 2019 5:35 PM
  • User1034446946 posted

    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......

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 9, 2019 9:01 PM
  • User-29703693 posted

    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.

    Thursday, October 10, 2019 2:37 AM
  • User61956409 posted

    Hi bank5,

    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.

    With Regards,

    Fei Han

    Thursday, October 10, 2019 2:43 AM