[英]ASP.NET MVC4 Unobtrusive validation localization
問題:
我有問題使用不顯眼的jquery驗證將默認消息本地化為隱式[Required]屬性。 我不想在我的模型中的每個int(和其他非可空類型)和相關的ressource文件中放置[Required]。 我想知道是否有人測試過ASP.NET MVC4 Dev Preview並注意到同樣的問題? 當我看到mvc代碼時,它看起來應該很有效。
嘗試解決方案:
在global.asax中添加:
DefaultModelBinder.ResourceClassKey = "ErrorMessages";
在PropertyValueInvalid和PropertyValueRequired的全局資源中有一個名為“ErrorMessages.resx”和“ErrorMessages.fr.resx”的資源文件。
有趣的信息:
我注意到的一件好事是,他們修改了“字段必須是數字”或“字段必須是日期”,而不是在內部密封類中進行硬編碼。
ClientDataTypeModelValidatorProvider.ResourceClassKey = "ErrorMessages";
如果在全局ressources文件夾和FieldMustBeNumeric / FieldMustBeDate中有一個名為“ErrorMessages.resx”和“ErrorMessages.fr.resx”的資源文件,則可以工作嗎?
我知道這是舊的,但是要將本地化消息放入元數據中,要將DataAnnotationsModelValidator子類化,並覆蓋GetClientValidationRules和Validate以提供您自己的消息。
您使用DataAnnotationsModelValidatorProvider.RegisterAdapterFactory注冊適配器。
我構建了一個包裝工廠構建器來創建工廠委托。 out參數在這里,因為我在循環中使用它,因為我通過反射發現程序集中的所有適配器,所以我需要獲取每個適配器的屬性類型以調用RegisterAdpaterFactory。 我可以內聯注冊,但在此之后我使用適配器/屬性信息做其他事情
public static class ModelValidationFactory
{
/// <summary>
/// Builds a Lamda expression with the Func<ModelMetadata, ControllerContext, ValidationAttribute, ModelValidator> signature
/// to instantiate a strongly typed constructor. This used by the <see cref="DataAnnotationsModelValidatorProvider.RegisterAdapterFactory"/>
/// and used (ultimately) by <see cref="ModelValidatorProviderCollection.GetValidators"/>
/// </summary>
/// <param name="adapterType">Adapter type, expecting subclass of <see cref="ValidatorResourceAdapterBase{TAttribute}"/> where T is one of the <see cref="ValidationAttribute"/> attributes</param>
/// <param name="attrType">The <see cref="ValidationAttribute"/> generic argument for the adapter</param>
/// <returns>The constructor invoker for the adapter. <see cref="DataAnnotationsModelValidationFactory"/></returns>
public static DataAnnotationsModelValidationFactory BuildFactory(Type adapterType, out Type attrType)
{
attrType = adapterType.BaseType.GetGenericArguments()[0];
ConstructorInfo ctor = adapterType.GetConstructor(new[] { typeof(ModelMetadata), typeof(ControllerContext), attrType });
ParameterInfo[] paramsInfo = ctor.GetParameters();
ParameterExpression modelMetadataParam = Expression.Parameter(typeof(ModelMetadata), "metadata");
ParameterExpression contextParam = Expression.Parameter(typeof(ControllerContext), "context");
ParameterExpression attributeParam = Expression.Parameter(typeof(ValidationAttribute), "attribute");
Expression[] ctorCallArgs = new Expression[]
{
modelMetadataParam,
contextParam,
Expression.TypeAs( attributeParam, attrType )
};
NewExpression ctorInvoker = Expression.New(ctor, ctorCallArgs);
// ( ModelMetadata metadata, ControllerContext context, ValidationAttribute attribute ) => new {AdapterType}(metadata, context, ({AttrType})attribute)
return Expression
.Lambda(typeof(DataAnnotationsModelValidationFactory), ctorInvoker, modelMetadataParam, contextParam, attributeParam)
.Compile()
as DataAnnotationsModelValidationFactory;
}
}
這也適用於MVC3,我也認為MVC2。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.