Is there a better way to consume multiple models than through ViewModels in ASP.NET MVC?

I work with ASP.NET MVC 2 and one of the examples that I follow is the official ASP.NET MVC Music Store on codeplex.

In the project example, they have this scenario: There are three models: Albums , Artists , Genres .

What makes me doubt it is how they relate to their views, for example, when they want to edit an album, for which he needs a list of all artists and genres from the database, so they create a ViewModel called StoreManagerViewModel

 public class StoreManagerViewModel{ public Album Album{get;set;} public List<Artists> Artists{get;set;} public List<Genre> Genres{get;set;} } 

This ViewModel is passed to the view and allows intellisense to see multiple models in the view.

This method seems that I will have an extra class for almost all relationships in my model: If I have a Discography class and I want to associate artists with Discography, I would have to make another ViewModel, as shown above.

However, I do not like to have two properties inside the Album method:

 public List<Artists> Artists{get;set;} public List<Genre> Genres{get;set;} 

Is there a better way to do this other than ViewModels? Is there a cleaner way?

+4
source share
5 answers

All the other answers on this page so far have left one important concept of the presentation model, that it separates the data layer from the presentation layer and removes the presentation ability to build queries that can crush your db.This is not only about intellisense and sending several models to the view. although these are other pluses.

For example, say that you want to upload 100 users to your site whose name begins with myNamePrefix . Then for each user you want to display a list of tags for which they have more than 10 upvotes. You can simply pass the Users list to your view, and then call the .Tags property, which then makes a circular trip to db for each of your 100 users. It can be good when db is on the same machine as the web server and you only get a few hits per day. But let's say you try to feed this data for different values ​​of myNamePrefix every second. You could probably find some creative ways to cache the results, but for the most part, it is best to populate your presentation model with all the data it needs (in this case, with a single query) and simply display the results. Remember that this is a viewing task for displaying data, not for retrieving it.

+4
source

The reason they decided to create a separate ViewModel instead of using an already created model, such as Albums , Artist or Genre , is because all 3 were needed. If only one is required, such as Albums , it would be nice to pass only Album or IList<Album> depending on usage.

In ASP.NET MVC, a model can be any object in the system that you want to send to the view. Even string , int and any other basic type.

In ASP.NET MVC 3, you can also use the dynamic keyword as a ViewModel, so you don’t even need to specify a type. However, you should probably avoid this until it is the last resort, because it is always better to have a statically typed ViewModel.

Finally, you don’t need to specify a model at all, you can install and retrieve information from the ViewModel dictionary. But this should probably be avoided for anything more complex than the simplest information, because, like dynamic , it is not statically typed.

+1
source

ViewModel exists for two reasons:

  • Intellisense Support
  • Instances in which passing a simple model is simply not enough for your look.

ViewModel allows you to submit several models in your opinion in cases where it makes sense - there are many cases when one simple model simply does not.

There are also cases when you need to send only one view to your model. Use it where it makes sense, and when you only need to send one model to the view (and you do not care about intellisense), then only send one model to this view.

0
source

when you use Linq to SQL, each model will have its children and parent loaded into the object (when your database is good). So in this case, you often need the original model in the form of editing or in the creation view. because your model will be made for you.

ViewModels is a clean way.

dirty way uses ViewData . well, "dirty." if you need a SelectList along with your model, using ViewData not dirty, this is the best way in these cases.

0
source

There is another way. You can use ChildActions.

Child actions can be cached, and each child action can be called from multiple views.

Using child actions may be the best solution if you have ViewModels that overlap. For example, if each view has a sidebar, you can display it with childaction, and since it is cached, db will not be requested for the sidebar model.

This is another way. Which is better depends on the problem you are trying to solve.

0
source

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


All Articles