How to validate start date and end date for a same attribute of Model

4

Problem description:

I need to create a form with a start date and end date to list the sales of a product by period.

Let's take this model as an example:

public class Produto
{
    [Display(Name = "ID")]
    public int IdProduto { get; set; }

    [Display(Name= "Descrição")]
    public string Descricao { get; set; }

    [Display(Name= "Data Venda")]
    public string DataVenda { get; set; }
}

In form, we will only have one text field with datainicial , another with datafinal , and the submit button.

My question is this: In this case, how would I do the form validations in the form of the datainicial and datafinal fields, in the VIEW, using Html.BeginForm , since the datainicial and datafinal are the same attribute in Model?

    
asked by anonymous 26.09.2014 / 16:23

1 answer

5

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.

        
    26.09.2014 / 18:06