views:

22

answers:

1

The following code was grabbed from MSDN: http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.stringlengthattribute.aspx

[MetadataType(typeof(ProductMetadata))]
public partial class Product
{

}

public class ProductMetadata
{

    [ScaffoldColumn(true)]
    [StringLength(4, ErrorMessage = "The ThumbnailPhotoFileName value cannot exceed 4 characters. ")]
    public object ThumbnailPhotoFileName;

}

How can I apply localize text (ex: from a Resource file) to the error message?

+1  A: 

Use the ValidationAttribute.ErrorMessageResourceType property to refer to your resource file, and the ValidationAttribute.ErrorMessageResourceName property to refer to the name of the string within that resource file. For example:

[StringLength(4, ErrorMessageResourceType = typeof(YourResourceFileHere), ErrorMessageResourceName = "NameOfStringInResourceFile")]

You can also check out this blog post if you need more examples.

Donut