【问题标题】:Validation type names in unobtrusive client validation rules must be unique. The following validation type was seen more than once: required不显眼的客户端验证规则中的验证类型名称必须是唯一的。多次看到以下验证类型:必需
【发布时间】:2015-01-21 04:57:17
【问题描述】:

我创建了自定义 ASP.Net MVC 模型验证,如下所示:

internal class LocalizedRequiredAttribute : RequiredAttribute, IClientValidatable 
{
    public List<string> DependentProperties { get; private set; }
    public List<string> DependentValues { get; private set; }
    public string Props { get; private set; }
    public string Vals { get; private set; }
    public string RequiredFieldValue { get; private set; }

    public LocalizedRequiredAttribute(string resourceId = "")
    {
        if (string.IsNullOrEmpty(resourceId))
            ErrorMessage = ResourcesHelper.GetMessageFromResource("RequiredValidationErrorMessage");
        else
            ErrorMessage = ResourcesHelper.GetMessageFromResource(resourceId);
    }

    public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
    {
        string msg = FormatErrorMessage(metadata.GetDisplayName());
        yield return new ModelClientValidationRequiredRule(msg); //Exception
    }
}
internal class LocalizedNumericRegularExpressionAttribute : RegularExpressionAttribute, IClientValidatable 
{
    public LocalizedNumericRegularExpressionAttribute(string resourceId = "") : base(@"^\d+$")
    {
        if (string.IsNullOrEmpty(resourceId))
            ErrorMessage = ResourcesHelper.GetMessageFromResource("NumberRequiredValidationErrorMessage");
        else
            ErrorMessage = ResourcesHelper.GetMessageFromResource(resourceId);
    }

    public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
    {
        string msg = FormatErrorMessage(metadata.GetDisplayName());
        yield return new ModelClientValidationRequiredRule(msg); //Exception
    }
}

以下是我的模型:

public class MyModel
{
   [LocalizedRequired]
   [LocalizedNumericRegularExpression]
   public int Emp_No { get; set; }
}

每当我使用上述模型导航到表单时,都会发生以下异常。

Validation type names in unobtrusive client validation rules must be unique. The following validation type was seen more than once: required

如果我删除IClientValidatable,上面的代码是可以的,但是客户端验证不起作用。

我的代码有什么问题?

【问题讨论】:

    标签: c# asp.net-mvc validation custom-validators


    【解决方案1】:

    我找到了解决方案,我们必须在 global.asax 中的 Application_Start 处添加以下代码

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

    【讨论】:

    • 在哪里可以找到 LocalizedRequiredAttribute 和 LocalizedNumericRegularExpressionAttribute ?
    • 这些是RegularExpressionAttributeRequiredAttribute定制的。仔细看问题
    【解决方案2】:

    您将 ValidationType 的值与 MVC 自动验证相同。因此,您必须在 ModelClientValidationRule 或其派生类中更改 ValidationType = "name unique" 的值。名称应避免 MVC 自动生成名称,例如“日期”、“必需”... 其他解决方案是通过将这些代码放在应用启动上来关闭自动验证

    DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-26
      • 1970-01-01
      • 2012-04-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多