Should ASP.Net MVC create a model for each view?

I am new to ASP.Net MVC, and I believe that I am making one model class for each view. For example. SignInModel, SignUpModel, EditProfileModel, etc.

Many of them are somewhat similar, with the same database settings, and then with several custom properties.

Is this really how MVC should be executed, or was I missing some aspect of it?

I understand that this may be subjective, but there should be โ€œbest practiceโ€ around it.

+8
asp.net-mvc
source share
2 answers

Is this really how MVC should be executed, or was I missing some aspect of it?

It is absolutely as it is assumed that it is MVC, and you will not miss any of its aspects.

A View the model per view.

You can still have base view model classes and use view model inheritance, but be careful, especially with validation rules, which can differ from different types. So, for example, if a property was required on one view, it would no longer be needed on another view, so if you used the model class of the base view and data annotations, you would be very fried.

Do not delay one second by creating view models, even if you repeat some properties. That view models are designed to => respond to specific presentation needs and take into account that these needs can change quite often, so having different presentation models may seem difficult at the beginning, but in the long run this is the best solution both in terms of service and and using IMHO best practices.

+15
source share

it really cleared up for me. Of course, I was doing something wrong, I did not think that so many modal representations would be the right way, it is just strange to duplicate many fields.

I felt pain and noticed a problem while trying to maintain validation. ,,

0
source share

All Articles