C # MVC: Return ViewModel or model class?

let's say I have a view where I currently only use model information from my User class.

Should I create an additional viewmodel shell or directly return an instance of the user class to the view?

Directly returned object:

return View(user); 

Wrapped in a ViewModel object:

 return View(new UserViewModel(user)); 

I am looking for the best practice here. Perhaps I need to return to watching later, but I do not know this yet. Could this be an argument to always use the optional ViewModel class?

Thank you for your help!

+4
source share
2 answers

No, you should not use the View Model if you use information only from the model. This is a simple situation. Create a strongly typed view and use your model directly.

+5
source

I tend to use a domain model until I need a view model. What for? Using a domain model is faster. Then, when the domain model no longer covers my need, I will reorganize, adding what I need, where I need it. I am a big fan of "quickly earned and reorganized."

This also applies to splitting models into multiple user controls.

+2
source

Source: https://habr.com/ru/post/1285919/


All Articles