当前位置: 首页 > 编程语言 > C#

为什么我不能将资源用作带有DataAnnotations的ErrorMessage?Share

时间:2023-04-11 12:05:25 C#

为什么我不能将资源用作带有DataAnnotations的ErrorMessage?为什么我不能这样做?[Required(ErrorMessage="*")][RegularExpression("^[a-zA-Z0-9_]*$",ErrorMessage=Resources.RegistrationModel.UsernameError)]publicstringUsername{get;放;}告诉我错误消息是什么?属性参数必须是属性参数类型的常量表达式、typeof表达式或数组创建表达式。使用ErrorMessage属性时,只能为其分配常量字符串或字符串文字。请使用ErrorMessageResourceType和ErrorMessageResourceName来指定您的资源。[RegularExpression("^[a-zA-Z0-9_]*$",ErrorMessageResourceType=typeof(Resources.RegistrationModel),ErrorMessageResourceName="UsernameError")]注意资源必须是public的(可以在资源编辑器中设置).设置资源访问publichttp://img402.imageshack.us/img402/504/98454371.jpg试试FluentModelMetaDataProvider。管理错误消息资源的强类型使用。它看起来像这样:使用System.Web.Mvc.Extensibility;命名空间UI.Model{publicclassStoreInputMetadata:ModelMetadataConfigurationBase{publicStoreInputMetadata(){Configure(m=>m.Id).Hide();配置(模型=>模型.Name).Required(Resources.Whatever.StoreIsRequired).MaximumLength(64,Resources.Whatever.StoreNameLengthSomething);}}}错误消息告诉我什么?属性参数必须是属性参数类型的常量表达式、typeof表达式或数组创建表达式。这是不言自明的。C#不是像Ruby这样的动态语言,在Ruby中你编写的类在运行时继承随机基类。?这是Skeet对此的评论。请查看此链接:http://code.msdn.microsoft.com/Getting-Started-WCF-RIA-1469cbe2/sourcecode?fileId=19242&pathId=774666288(linkisbroken,butleftforattribution)publicsealedpartialclassRegistrationData{[Key][Required(ErrorMessageResourceName="ValidationErrorRequiredField",ErrorMessageResourceType=typeof(ErrorResources))][Display(Order=0,Name="UserNameLabel",ResourceType=typeof(RegistrationDataResources))][RegularExpression("^[a-zA-Z0-9_]*$",ErrorMessageResourceName="ValidationErrorInvalidUserName",ErrorMessageResourceType=typeof(ErrorResources))][StringLength(255,MinimumLength)=4,ErrorMessageResourceName="ValidationErrorBadUserNameLength",ErrorMessageResourceType=typeof(ErrorResources))]publicstringUserName{get;放;这意味着您无法在运行时确定传递给属性的参数的值,它必须在编译时才能确定,因此该值将嵌入到程序集中。错误信息值必须是常量表达式。有关信息,属性参数只能是bool、byte、char、short、int、long、float、double、string、System.Type和枚举类型。您应该查看此属性的ErrorMessageResourceName和ErrorMessageResourceType属性。它们允许从资源中提取错误消息。以上是C#学习教程:为什么不能将资源作为ErrorMessagewithDataAnnotations使用?如果所有分享的内容对你有用,需要进一步了解C#学习教程,希望大家多多关注。本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处:

最新推荐
猜你喜欢