ASP.NET MVC 2 - Property Validation

I am new to MVC and ask a question about validation. Is there a way to dynamically set an error message?

For example, how can I achieve the following (ignore the hardcoded 50, this may be due to Web.config or specific to the current log).

[MetadataType(typeof(DocumentValidation))] public partial class Document { public class DocumentValidation { private const int MaxLength = 50; [Required(ErrorMessage = "Document Title is required")] [StringLength(MaxLength, ErrorMessage = "Must be under " + MaxLength.ToString() + " characters")] public string Title { get; set; } } 

}

Thanks,

0
source share
3 answers

Refuse IDataErrorInfo and this question I asked about IDataErrorInfo vs. DataAnnotations .

+1
source

This should be possible with dynamic attributes, but associated with some tricks:

Dynamic Attributes in C #

+1
source

Depending on how dynamically you are trying to change ErrorMessage. This might be one of the solutions to your problem: haacked.com - Localization of ASP.NET MVC Validation

This is a good guide to getting a localized error message from resources.

0
source

All Articles