locked
Range validation and error message RRS feed

  • Question

  • User-501297529 posted

    I have a validation for range for a property in my model and I'm trying to get the message to display when the range isn't 1 to 10 but it doesn't show on the page. This is what I have so far:

            [Required]
            [Range(1, 10, ErrorMessage = "Display Order be between 1 - 10")]
            [DisplayName("Display Order")]
            public int? DisplayOrder { get; set; }

    Thursday, August 8, 2019 6:55 PM

Answers

  • User475983607 posted

    Keep in mind that you've defined the DisplayOrder as a nullable int which contradicts required and a range validation.  It is not possible to have a null DisplayOrder if a DisplayOrder is required.

    Other than than the logical error there is not enough information to provide an accurate solution.  For example, are you checking validation errors in the action as illustrated in the the reference documentation?

    https://docs.microsoft.com/en-us/aspnet/core/mvc/models/validation?view=aspnetcore-2.2

    If you are still having issue then post enough code so a forum member can reproduce the issue.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 8, 2019 7:22 PM

All replies

  • User475983607 posted

    Keep in mind that you've defined the DisplayOrder as a nullable int which contradicts required and a range validation.  It is not possible to have a null DisplayOrder if a DisplayOrder is required.

    Other than than the logical error there is not enough information to provide an accurate solution.  For example, are you checking validation errors in the action as illustrated in the the reference documentation?

    https://docs.microsoft.com/en-us/aspnet/core/mvc/models/validation?view=aspnetcore-2.2

    If you are still having issue then post enough code so a forum member can reproduce the issue.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 8, 2019 7:22 PM
  • User711641945 posted

    Hi bootzilla,

    You means that the message did not show on the page and the validation did not pass.If so,you should check the following code:

    <div class="form-group">
           <label asp-for="DisplayOrder" class="control-label"></label>
           <input asp-for="DisplayOrder" class="form-control" />
           <span asp-validation-for="DisplayOrder" class="text-danger"></span>
    </div>

    If the validation passed, I suggest that you could share a simple demo to reproduce your issue.

    Best Regards,

    Rena

    Friday, August 9, 2019 7:22 AM