2011-11-30 30 views
6

Vấn đề:ASP.NET MVC4 nội địa xác nhận Không phô trương

Tôi có vấn đề nhận được các tin nhắn mặc định được bản địa hoá cho tiềm ẩn [Bắt buộc] thuộc tính sử dụng validation jquery không phô trương. Tôi không muốn đặt [Bắt buộc] trên mọi int (và các kiểu không nullable khác) trong mô hình của tôi và tệp tin ressource được liên kết. Tôi tự hỏi nếu có ai đã thử nghiệm xem trước ASP.NET MVC4 Dev và nhận thấy cùng một vấn đề? Khi tôi nhìn vào mã mvc nó rõ ràng có vẻ như nó sẽ làm việc.

giải pháp đã thử:

gia tăng trong global.asax:

DefaultModelBinder.ResourceClassKey = "ErrorMessages"; 

Có một tập tin tài nguyên được gọi là "ErrorMessages.resx" và "ErrorMessages.fr.resx" trong nguồn lực toàn cầu với PropertyValueInvalid và PropertyValueRequired.

thông tin thú vị:

Một điều tốt tôi đã nhận thấy là họ cố định "Field phải là một số" hoặc "Field phải là một ngày" không bị cứng được mã hóa trong một lớp kín nội bộ.

ClientDataTypeModelValidatorProvider.ResourceClassKey = "ErrorMessages"; 

Liệu có tác dụng nếu bạn có một tập tin tài nguyên được gọi là "ErrorMessages.resx" và "ErrorMessages.fr.resx" trong ressources toàn cầu thư mục và FieldMustBeNumeric/FieldMustBeDate

+2

Bạn đang nói điều này làm việc trong MVC2/3 và bị hỏng trong bản xem trước v4? – RickAndMSFT

Trả lời

2

Tôi biết điều này là cũ, nhưng để nhận thư được bản địa hóa vào siêu dữ liệu là phân lớp DataAnnotationsModelValidator và ghi đè GetClientValidationRules và Xác thực để cung cấp thư của riêng bạn.

Bạn đăng ký bộ điều hợp bằng DataAnnotationsModelValidatorProvider.RegisterAdapterFactory.

Tôi đã xây dựng trình tạo nhà máy bao bọc để tạo đại biểu nhà máy. Các tham số ra là ở đây như tôi sử dụng này bên trong một vòng lặp như tôi phát hiện tất cả các adapter trong lắp ráp thông qua sự phản ánh, vì vậy tôi cần phải nhận được loại thuộc tính cho mỗi adapter để gọi RegisterAdpaterFactory. Tôi có thể inlined việc đăng ký nhưng tôi làm những thứ khác sau này sử dụng bộ chuyển đổi thông tin/attribute

public static class ModelValidationFactory 
{ 
    /// <summary> 
    /// Builds a Lamda expression with the Func&lt;ModelMetadata, ControllerContext, ValidationAttribute, ModelValidator&gt; 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; 
    } 
} 

này cũng hoạt động trong MVC3, và tôi nghĩ rằng MVC2 là tốt.

Các vấn đề liên quan