C # Decimal field accept value equal to or greater than 0 in validation

2

I'm having problems validating my fields of type decimal using Razor, when I try to enter 0 in the field it gives the following error message.

  

The field XXXX must be a number.

Below is my class and how I enter the field with Razor

public class ClasseViewModel
{
    [Required(ErrorMessage = "Campo Obrigatorio")]
    [Display(Name = "Campo Decimal")]
    public decimal? CampoDecimal { get; set; }
}

Razor:

@Html.LabelFor(model => model.CampoDecimal, htmlAttributes: new { @class = "control-label" })
@Html.EditorFor(model => model.CampoDecimal, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.CampoDecimal, "", new { @class = "text-danger" })

How would you make the validation of whether the field is numeric or would not accept the value 0.

    
asked by anonymous 15.12.2017 / 14:31

1 answer

3

Since everything is with Razor, use DataAnnotations to set the limit of 1 pair to infinity (can be another number) and remove ? if the parameter is required

[Range(1, Double.PositiveInfinity)]
public decimal CampoDecimal { get; set; }

For more details: data annotations

    
15.12.2017 / 16:13