2011-08-26 56 views
1

我在我的模型中使用ASP.NET MVC 3和数据注解,并且想要从数据库中检索错误消息。所以我写了继承属性:ASP.NET MVC - “验证类型名称必须是唯一的。”

public class LocalizedRequiredAttribute : RequiredAttribute 
{ 
    public LocalizedRequiredAttribute(){} 

    public override string FormatErrorMessage(string name) 
    { 
     return GetByKeyHelper.GetByKey(this.ErrorMessage); 
    }  
} 

public class LocalizedRegularExpressionAttribute : RegularExpressionAttribute 
{ 
    public LocalizedRegularExpressionAttribute(string pattern) : base(pattern){} 

    public override string FormatErrorMessage(string name) 
    { 
     return GetByKeyHelper.GetByKey(this.ErrorMessage); 
    }  
} 

我写2“适配器”这些属性,以使客户端验证,比如这个:

public class LocalizedRequiredAttributeAdapter : DataAnnotationsModelValidator<LocalizedRequiredAttribute> 
{ 
    public LocalizedRequiredAttributeAdapter(ModelMetadata metadata, ControllerContext context, LocalizedRequiredAttribute attribute) 
     : base(metadata, context, attribute) 
    { 
    } 

    public static void SelfRegister() 
    { 
     DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(LocalizedRequiredAttribute), typeof(RequiredAttributeAdapter)); 
    } 

    public override IEnumerable<ModelClientValidationRule> GetClientValidationRules() 
    { 
     return new[] { new ModelClientValidationRequiredRule(ErrorMessage) }; 
    } 
} 

在我的Global.asax我有这两条线:

 LocalizedRegularExpressionAttributeAdapter.SelfRegister(); 
     LocalizedRequiredAttributeAdapter.SelfRegister(); 

我得到一个异常“低调中的客户端验证规则验证类型名称必须是唯一的下列验证类型被视为不止一次:需要”时,我的模型呈现HTM L为此属性:

[LocalizedRequired(ErrorMessage = "global_Required_AccountName")] 
    [LocalizedRegularExpression(User.ADAccountMask, ErrorMessage = "global_Regex_AccountName")]   
    public string AccountName { get; set; } 

什么是错?

回答

0

我猜,也许读取行:

DataAnnotationsModelValidatorProvider.RegisterAdapter(
     typeof(LocalizedRequiredAttribute), 
     typeof(RequiredAttributeAdapter)); 

应改为:

DataAnnotationsModelValidatorProvider.RegisterAdapter(
     typeof(LocalizedRequiredAttribute), 
     typeof(LocalizedRequiredAttributeAdapter)); 
+0

是的,你是对的,但与此我有同样的InvalidOperationException。 – asdfghjkl

1

我基本上有同样的问题,我设法用下面一段代码来解决这个问题:

using System; 
using System.Web.Mvc; 

而且有效性规则:

public class RequiredIfValidationRule : ModelClientValidationRule 
{ 
    private const string Chars = "abcdefghijklmnopqrstuvwxyz"; 

    public RequiredIfValidationRule(string errorMessage, string reqVal, 
     string otherProperties, string otherValues, int count) 
    { 
     var c = ""; 
     if (count > 0) 
     { 
      var p = 0; 
      while (count/Math.Pow(Chars.Length, p) > Chars.Length) 
       p++; 

      while (p > 0) 
      { 
       var i = (int)(count/Math.Pow(Chars.Length, p)); 
       c += Chars[Math.Max(i, 1) - 1]; 
       count = count - (int)(i * Math.Pow(Chars.Length, p)); 
       p--; 
      } 
      var ip = Math.Max(Math.Min((count) % Chars.Length, Chars.Length - 1), 0); 
      c += Chars[ip]; 
     } 

     ErrorMessage = errorMessage; 
     // The following line is where i used the unique part of the name 
     // that was generated above. 
     ValidationType = "requiredif"+c; 
     ValidationParameters.Add("reqval", reqVal); 
     ValidationParameters.Add("others", otherProperties); 
     ValidationParameters.Add("values", otherValues); 
    } 
} 

我希望这有助于。

相关问题