Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

StringLengthAttribute and Localized text

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?

like image 986
burnt1ce Avatar asked Aug 03 '10 17:08

burnt1ce


1 Answers

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.

like image 123
Donut Avatar answered Oct 29 '22 20:10

Donut