13
I’m used to the MVC approach of frameworks as Cakephp and Laravel. Now that I’m getting deeper into ASP.NET MVC, I’d like to understand what this Viewmodel is for.
What is the purpose of the Viewmodels?
13
I’m used to the MVC approach of frameworks as Cakephp and Laravel. Now that I’m getting deeper into ASP.NET MVC, I’d like to understand what this Viewmodel is for.
What is the purpose of the Viewmodels?
10
In C# everything needs a type. Well, currently it is even possible to work more dynamically, but it is not the point. Types are defined by classes.
In ASP.NET MVC it is customary to type the views also. That is, you define that a view will receive an object and that this object must be of a certain type that, as already said, is defined by a class.
Starting from this point, I will try to explain using an example, to make it easier to visualize.
Imagine you have a model (a type, a class) that represents a database table called a person.
public class Pessoa { }
Now imagine that this person may have a name and one or more cars, therefore, the class Pessoa
will have two properties
public class Pessoa
{
public string Nome { get; set; }
public List<Carro> Carros { get; set; }
}
And the car class is defined as follows
public class Carro
{
public string Modelo { get; set; }
}
Well, now imagine that you have the following task: to create a view that shows the names of all registered persons and the model of all their cars separated by comma.
You can simply create a view using the type Pessoa
and treat the models of the cars in the code itself Razor.
@model List<Pessoa>
@foreach(var pessoa in Model)
{
<p>@(pessoa.Nome) - @(string.Join(", ", pessoa.Carros.Select(c => c.Modelo)))</p>
}
Great, you’re ready, but it would be a lot smarter if you sent it to view only one type containing two strings. That’s where the one comes in view model.
public class PessoaCarrosViewModel
{
public string NomePessoa { get; set; }
public string ModelosCarros { get; set; }
}
Following this idea, the code of view would be
@model List<PessoaCarrosViewModel>
@foreach(var pv in Model)
{
<p>@(pv.NomePessoa) - @(pv.ModelosCarros)</p>
}
Obviously at some point in the code there will have to be a way to map a data set (models, for example) to a view model.
Of course my example is extraordinarily simple and probably not even really necessary the use of a view model, but her idea is just this, you have a type that contains mixed information of other types to present in view.
10
I’ll make an analogy that I think it’s very easy to understand what a view model.
Think of a database, but only as a metaphorical relationship, not that you need to have a relationship between them. The model of your MVC application is the database table. The view model is the equivalent of what we know of view in the database. Simple, right?
In Cakephp and Laravel the concept is this? If it is, in ASP.NET MVC or Core (which I advise most) it’s the same thing.
Inclusive the AP has already had a prior on the use of views SQL in the MVC, where the approach was a little different.
Sure, there are concrete differences between the concepts, but basically this is it.
As the name says, model view is a model of vision.
What is the normal model, the M of the MVC?
It determines which data will be available to you in the database you are working on. Note that I have not mentioned database, groundwork can be either way.
The model can be used directly in view no problem at all.
I imagine you understand well that the view is where the actual data presentation.
But in anything that comes out of CRUD very simple, and even in some cases like this, the model cannot be used directly in a simple way. It would need to juggle to work. In some cases it wouldn’t even work.
The solution is to create an "alternative" model that will be used in the view. That is to say view model is a model that only meets the specific need for one or more visions. We can say that it is equal to model pure, but it does not link directly to the database.
In the view model you create a structure with the data you will use in view, which may be only the subset of model fields, may have fields created on demand that will be easily used in the view (remembering that the views should not be processed except to control its own construction). But it can also provide data from other objects of the model adopted. Generally the MVC has no mechanism to pass different objects to view directly, you need to create a new model with what you need.
Remembering that view models are usually created in the controller, even if indirectly.
Is almost duplicate of View Model must have related classes?.
More information on the MVS where it was inspired its use, but there in a more complete.
There is a cool example in response in the OS. To view model:
public class CountyViewModel {
[HiddenInput]
public int? CountyId { get; set; }
[DisplayName("County Name")] [StringLength(25)]
public string CountyName { get; set; }
[DisplayName("County URL")] [StringLength(255)]
public string URL { get; set; }
}
public class ListAllCountiesViewModel {
public string CountyName { get; set; }
public IEnumerable<County> ListAllCounty { get; set; }
}
public class PropertyViewModel {
public ListAllCountiesViewModel _listAllCountyViewModel { get; set; }
public CountyViewModel _countyViewModel { get; set; }
}
Usually my database returns are mapped to the Model, then I use the auto mapper to map the model to viewModel and send to the page. This is the idea?
What a difference to DTO @Maniero?
@Marconi I don’t know if I understood the idea well, but I think not, I made an edition to see if it is clearer. DTO is not this, DTO is more for the model same. The viewmodel has no direct relation to the database, she uses the application model to assemble her own model. the DTO has direct relation to the database (usually a database).
Answer for us? D Difference between DTO and Viewmodel?
Browser other questions tagged asp.net-mvc mvc viewmodel
You are not signed in. Login or sign up in order to post.
The viewmodel , allow you to create a template according to what you need in your view. and also allow you not to have coupling with your domain.
– Marco Souza
This concept is quite similar @Wallace: Use of DTO and Viewmodel in ASP.Net MVC project
– Marconi