Return the model class or VistaModel?

1

I have a design problem, I currently have a view that I only use information from my user model classe. What in the view I do not need all the information of the model what should create an extra class viewmodel with the necessary information or can I rethrust the user class directly in view?

return View(user);

or

return View(new UserViewModel(user));

Since in the case that I have to do a single vewModel for that view, when it is in other views that need another type of user information, I will have to create another viewmodel with the properties that are necessary for the view?

Thanks for the help!

    
asked by gvivetapl 13.07.2016 в 13:14
source

2 answers

2

You can use the User class as a model even though you have more information than the view requires. It is a decision you must make during development: when to create a new class as a view model and when to reuse an existing one.

To take this decision, the issues that you will have to consider are:

  • Clarity of the code
  • If the view needs additional information to the one contained in the class (in this case it would be advisable to create a view model)
  • Possible security problems

When I talk about security problems I mean you have to take into account that information received from the browser can include properties that you do not use in the view.

For example, if the User class has the properties Code, Name, Surname, Age, Email. You can create a view in which you only show controls to edit, for example, the first and last name (Name and Surname). However, if a malicious user adds to the information in the post when submitting the form a value with an Email key, MVC will automatically bin this value to the property of the User object received in the controller. You should then take into account in your code that the value of this property could have been modified by the user even if you have not given the possibility to edit it in the form.

If you use a view model class with only the Name and Surname properties this case could not occur.

    
answered by 13.07.2016 в 13:35
2

> > > Should I create an extra viewmodel class with the necessary information or can I rethink the user class directly to the view?

It depends, there is no rule that you have to convert the entity to a viewmodel to use it in the view, although it would be advisable, mostly because of the definition of validations using Data Annotations .

When you add attributes in the properties of the class you are modifying it and this would be advisable not to do it on the entity that you then use in the business or persistence layer.

> > when in other views that require other types of user information, will I have to create another viewmodel with the properties that are necessary for the view?

Exactly, it is more possible that you have a viewmodel with minimum data to list in a table and then a more detailed one with the properties for the edition

Remember that you can use inheritance between these classes and thus not define all the properties

    
answered by 13.07.2016 в 15:40