2011-01-22 53 views
1

ASP.NET MVC有一個很好的功能,稱爲DataAnnotations,可以簡化用戶輸入驗證。我無法找到一種處理內置數據註釋的方法,這樣如果用戶正在運行我的應用程序的西班牙語版本,驗證消息將會更改。有人可以舉一個考慮多種語言的例子嗎?DataAnnotations和多語言的Web應用程序

+1

參見這篇文章:http://stackoverflow.com/questions/3758512/localize-data-annotations-default-messages-required-stringlength -等等 – 2011-01-22 08:14:37

回答

3

它會變得相當難看得很快。

public class User 
{ 
    [Required(ErrorMessageResourceName = "Validation_Required", ErrorMessageResourceType = typeof(ModelTranslations))] 
    public int Id { get; set; } 

    [Required(ErrorMessageResourceName = "Validation_Required", ErrorMessageResourceType = typeof(ModelTranslations))] 
    [StringLength(40, ErrorMessageResourceName = "Validation_StringLength", ErrorMessageResourceType = typeof(ModelTranslations))] 
    public string FirstName { get; set; } 

    [Required(ErrorMessageResourceName = "Validation_Required", ErrorMessageResourceType = typeof(ModelTranslations))] 
    [StringLength(40, ErrorMessageResourceName = "Validation_StringLength", ErrorMessageResourceType = typeof(ModelTranslations))] 
    public string LastName { get; set; } 
} 

我在我的博客更漂亮的解決方案:http://blog.gauffin.org/2010/11/simplified-localization-for-dataannotations/