In some cases your domain class / database model is easily adaptable to view , in other cases not.
It is recommended that you create view templates to take care of specific cases.
For example, when you create a new web application with the template that contains the "Individual User Account" you will have a LoginViewModel
class for the Login screen, another class RegisterViewModel
to the log screen, etc. All in the AccountViewModels.cs file in the Model template of the Asp.Net MVC template.
So I say, and it is recommended that you create a default template for each view.
So, you would 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 your view , it looks something like this:
@model App.Web.Model.ProdutoVendasPorPeriodoViewModel
...
@Html.TextBoxFor(x => x.DataInicial)
....
@Html.TextBoxFor(x => x.DataFinal)
Following this pattern, you would have advantages like:
Do not need to create gambiarras for views that do not represent the same model of the domain class;
It would also not have the attributes DisplayAttribute
"dirtying" your
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 the Initial Date and End Date values with the RequiredAttribute
attribute and the DisplayAttribute
attribute to have a more "appropriate" value for view .
Palliative means
You can still create a non-typed view , that is, without informing @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.
That way you would receive the values in the parameters and the validation would be left to you.