In some cases your domain class/database model is easily adaptable to the view, in other cases not.
It is recommended to create vision models to handle specific cases.
For example, when you create a new web application with the template that contains the "Individual User Account" you will have a class LoginViewModel
for Login screen, another class RegisterViewModel
for registration screen, etc. Everything inside the file Accountviewmodels.Cs in the briefcase Model of the template Asp.Net MVC.
Therefore, I say, and it is recommended, that you create a standard template for each view.
Then you’d have something like:
public class ProdutoVendasPorPeriodoViewModel // nome nada exagerado
{
[Required(ErrorMessage = "Informa a data inicial")]
[Display(Name= "Data Inicial")]
[DataType(DataType.Date)]
public DateTime DataInicial { get; set; }
[Required(ErrorMessage = "Informa a data final")]
[Display(Name= "Data Final")]
[DataType(DataType.Date)]
public DateTime Final { get; set; }
}
And in his view, would look something like this:
@model App.Web.Model.ProdutoVendasPorPeriodoViewModel
...
@Html.TextBoxFor(x => x.DataInicial)
....
@Html.TextBoxFor(x => x.DataFinal)
By following this pattern, you would have advantages such as:
- do not need to create tricks to views that do not represent the same model of the domain class;
- I wouldn’t have the attributes either
DisplayAttribute
"soiling" his
domain class;
- You can have well customized classes with attributes that would help you validate and model the view, such as the example I gave, requiring Start Date and End Date values with the attribute
RequiredAttribute
and the DisplayAttribute
which has come to have a value more "appropriate" for the view.
Palliative media
You can even create a view not "typed", that is, without informing the @model
.
The fields you could create manually or with the use of helpers as well.
Example:
<input type="text" id="DataInicial" name="DataInicial" .. />
...
<input type="text" id="DataInicial" name="DataInicial" .. />
... ou
@Html.TextBox("DataInicial", "")
@Html.TextBox("DataFinal", "")
For this your Action should be signed as this example:
public ActionResult Relatorio (string DataInicial, string DataFinal)
{
...
}
That is, having the parameters with the name of the fields.
This way you would receive the values in the parameters and the validation would be on your own.
Can I be using the model itself in some cases and for others the viewmodel? Or is it recommended that I always use viewmodel?
– Diego Zanardo
I’m in the middle of a project that doesn’t have Viewmodel. So I use Displayattribute for example, right in the domain. But now for a class would need to use viewmodel, and it would be a rework to be changing everything I’ve done to viewmodels.
– Diego Zanardo
Thank you @Tiagosilva. My question was precisely whether it was common to create several classes besides the Product Model. Thank you for the clarification. I was already starting the "Half Active" that you spoke in the answer above. But I think I’ll take advantage that I’m still at the beginning of the project and do it the right way.
– Flavio