Is it nice to use many view modes in asp.net mvc

I worked on the project of my beloved on the side to get to know asp.net mvc better. My question is about viewmodels in mvc. I understand that the controller must handle the interaction between the view and the model. I feel that I need to create viewmodel classes to combine information from models in order to get to the view.

Is this a bad practice? Should I do more logic elsewhere and reduce view modes?

At the moment, I almost have a viewmodel for almost every one of my basic views. But I definitely don't want the view to access the model directly.

+7
source share
5 answers

No, all of your views should be strongly typed, so for each view, one viewing model is best practice. Here is a very good article on viewing models.

+6
source

The more viewing models you use, the better. Viewmodels allow you to create a single object that can contain various types of data from your models. This is very useful when creating templates. It is also very useful when using jQuery and Ajax, as it is a good way to transfer data to your controller and then directly to the DOM. In my opinion, use as many viewmodel as possible.

Another thing you can consider is to try to better design your model. I personally try to create my model as a sql database and follow normalization forms. You do not need to create a new viewing model for each view in addition to the existing models. If you need to pass information to a view that is not part of the model you are using, use ViewData or ViewBag. They are passed as an object, so you have to pass them to the appropriate class.

+2
source

Nothing wrong with that. As you get closer to your viewmodel, closer to the view is better.

The model cannot display a View match, so the ViewModel class is used for this.

0
source

Great answers here, and here is my opinion on why many presentation models are not a problem. Your ViewModels are a great way to separate your clean data access objects or even your domain objects from your presentation level and provide a dead-end version of these objects for your views to consume.

Your views should be silly and for me personally, what I'm trying to achieve is a massage of my domain model in the ViewModel for my viewing.

0
source

Using a large number of viewmodels models creates a mess in your project. It is much better to just create a Tuple in the controller and then pass it.

-3
source

All Articles