Asp – Problem with DataAnnotations and MVC1: Required attribute is ignoring the ErrorMessage property

asp.net-mvcdata-annotationsvalidation

I'm validating the properties of a linq to sql entity using DataAnnotations, the properties are validating fine but the Required[ErrorMessage="error message"] ErrorMessage attribute is being ignored and instead I'm getting the default error message.

Here's the code I have so far:

    [DisplayName("Nombre")]
    [Required( ErrorMessage = "Este campo es requerido" )]
    public string Name
    {
        get;
        set;
    }

    [Required( ErrorMessage = "Este campo es requerido" )]
    [Range( 0, 1000000, ErrorMessage = "Debe insertar un valor entre {1} y 1,000,000" )]
    public decimal Maximum
    {
        get;
        set;
    }

    [Required( ErrorMessage = "Este campo es requerido" )]
    [Range( 0, 100, ErrorMessage = "Debe insertar un valor entre {1} y {2}" )]
    public byte Periods
    {
        get;
        set;
    }

Best Answer

Check to see if it's a namespace issue. I just fixed my problem by putting the Metadata "buddy" class into the same namespace as the Model L2S class, even though I thought I had everything referenced properly. I wanted to put the metadata classes into their own namespace for organizational purposes but it didn't seem to like that. FWIW, I'm running on .net 3.5, VS 2008, MVC 2 RC.