Custom validation error message if user places non-chip string in int field

This question should have been asked before, but I think the search terms are too general for me to find the answer I'm looking for, so I'll ask it again.

I have a model with a property intand range annotation.

If the user enters something other than int, the check message responds with The value '<bad data>' is not valid for '<property name>'... which is fine, but I want to provide a bit more feedback, i.e. Expecting an integer value in this field..

Since this check is not performed before other validators pay attention, I do not know how (or, if possible) to override this default message for verification.

What are my options?

for the request, I send the code, but there are not many:

[Range(0,65535, ErrorMessage="Port must be between 0 and 65535.")] 
public int Port { get; set; }

, , RangeAttribute. .

+5
3

, :

[MyAnnotation(...., ErrorMessage = "My error message")]
public int myInt { get; set; }

()?

: Misread - : ErrorMessage ASP.NET MVC? "data-val-number" MVC, @Html

+3

IValidatableObject . Validate. .

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;

namespace MvcApplication1.Models
{
    public class Alok : IValidatableObject
    {
        [Display(Name = "Property1")]
        [Required(AllowEmptyStrings = false, ErrorMessage = "Property1 is required.")]
        public int Property1 { get; set; }

        [Display(Name = "Property2")]
        [Required(AllowEmptyStrings = false, ErrorMessage = "Property2 is required.")]
        public int Property2 { get; set; }

        public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
        {
            if (Property1 < Property2)
            {
                yield return new ValidationResult("Property 1 can't be less than Property 2.");
            }
        }
    }
}
+3

. , OP, deafult, , linnk , . . , .

+1

All Articles