ASP.NET MVC - “验证类型名称必须是唯一的”。

时间:2011-08-26 10:00:47

标签: asp.net-mvc data-annotations

我在模型中使用ASP.NET MVC 3和Data Annotations,并希望从数据库中检索错误消息。所以我写了继承属性:

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();

我得到一个例外“不显眼的客户端验证规则中的验证类型名称必须是唯一的。以下验证类型被多次看到:需要”当我的模型呈现此属性的HTML时:

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

有什么问题?

2 个答案:

答案 0 :(得分:1)

我基本上遇到了同样的问题,我设法使用以下代码解决了这个问题:

using System;
using System.Web.Mvc;

ValidationRule:

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);
    }
}

我希望这会有所帮助。

答案 1 :(得分:0)

我猜这可能是一行:

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

应阅读:

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