我在写一个应用程序,它从数据库加载语言文本以启用国际化,使用viewmodels中的键来查找正确的文本。我从http://www.codeproject.com/Tips/514321/A-Simple-and-Effective-Way-to-Localize-ASP-Net-MVC修改了MyLocalizationprovider的一个版本,我的修改后的代码可以在这里找到:https://gist.github.com/TheConfuser/5f4a64ebe13254e7d45b。代码通过元数据属性并替换标题(DisplayName),占位符(Prompt),错误消息(ErrorMessage)和验证错误消息(ErrorMessage)。我为ErrorMessage提取DisplayAttribute,提示和名称以及ErrorMessage的ValidationAttribute,以便在两个字段之间进行验证,本例中为密码。我得到这样的密钥:替换元数据属性中的regularexpression字符串
if (typeName.Equals("DisplayAttribute"))
{
nameKey = ((DisplayAttribute) attr).Name;
placeholderKey = ((DisplayAttribute) attr).Prompt;
...
然后我用我的数据库中的值替换键。我的ViewModels看起来像如下:
[Required(ErrorMessage = "changepassword_newpassword_error")]
[DataType(DataType.Password)]
[Display(Prompt = "changepassword_newpassword_placeholder", Name = "changepassword_newpassword_headline")]
[RegularExpression(@"^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,12}", ErrorMessage = "changepassword_newpassword_regexerror")]
public string NewPassword { get; set; }
这工作正常占位符,标题,errormessages的字段和验证,但是,我对如何找到正则表达式还没有任何想法。我认为它会作为ErrorMessage的ValidationAttribute存在,但我在那里找不到任何东西。请参阅http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.validationattribute(v=vs.110).aspx。
哪里可以找到regularexpression-string的字段,它甚至存在吗? 或者我应该尝试写我自己的属性(如Set RegularExpression Dynamically in Model),如果有的话,任何人都可以帮助我?
任何输入,任何人? – confuse 2014-11-18 07:56:34