Skip to content

Incorrect results of RangeAttribute #47434

@martinmikulka

Description

@martinmikulka

Hello, I've encountered a strange behaviour of the RangeAttribute when attempting to set a negative value. Values between -0.5 and 0 are evaluated as valid.

Model:

    public class MyModel
    {
        [Range(0, 1)]
        public double Test { get; set; }
    }

Controller:

    [HttpPost]
    public IActionResult Test(MyModel model)
    {
        var tests = new double[] { -1, -0.6, -0.51, -0.5, -0.49, -0.4, -0.1, 0 };
        foreach (var test in tests)
        {
            model.Test = test;
            ModelState.Clear();
            TryValidateModel(model);
            Console.WriteLine($"Value: {model.Test}, Result: {ModelState.IsValid}");
        }

        return Ok();
    }

Results:
Value: -1, Result: False
Value: -0,6, Result: False
Value: -0,51, Result: False
Value: -0,5, Result: True
Value: -0,49, Result: True
Value: -0,4, Result: True
Value: -0,1, Result: True
Value: 0, Result: True

dotnet --info
SADA .NET SDK:
 Version:   6.0.407
 Commit:    d8659ea151

Runtime:
 OS Name:     Windows
 OS Version:  10.0.22621
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\6.0.407\

global.json file:
  Not found

Host:
  Version:      6.0.15
  Architecture: x64
  Commit:       5edef4b20b

Metadata

Metadata

Assignees

No one assigned

    Labels

    ✔️ Resolution: AnsweredResolved because the question asked by the original author has been answered.Status: Resolvedarea-mvcIncludes: MVC, Actions and Controllers, Localization, CORS, most templatesquestion

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions