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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With