Search code examples
c#razorasp.net-mvc-5viewmodelfluentvalidation

FluentValidation range throws InvalidOperationException in Razor view


i'm working with ASP MVC 5, and i'm pretty new using FluentValidation.

I'm trying to validate a simple range.

I have a class like this

public int Property { get; set; }

And this is my validation

RuleFor(x => x.Property).NotEmpty()
                .GreaterThanOrEqualTo(1)
                .LessThanOrEqualTo(20);

When i want to use it in razor view using EditorFor

@Html.EditorFor(x => x.FactorK)

It throws a InvalidOperationException

System.InvalidOperationException: The names of the validation types in discrete client validation rules must be unique. The following type of validation has been observed more than once: range

The strange thing is that when trying to use only one method (GreaterThanOrEqual or LessThanOrEqual) it works ok.

For Example,

RuleFor(x => x.Property).NotEmpty()
                .GreaterThanOrEqualTo(1);

This works.

What am i doing wrong?

Thanks!


Solution

  • The error message explain the reason of error, both GreaterThanOrEqualTo and LessThanOrEqualTo are of "range" validation type. Try to use InclusiveBetween instead.