Validating start date and end date for the same Model attribute

Asked

Viewed 617 times

4

Description of the problem:

I need to create a form with an initial date and end date to list sales of a product per 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 the form, we will only have a text field with the datainicial, another with the datafinal, and the button submit.

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

1 answer

5


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:

  1. do not need to create tricks to views that do not represent the same model of the domain class;
  2. I wouldn’t have the attributes either DisplayAttribute "soiling" his domain class;
  3. 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?

  • 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.

  • 2

    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.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.