验证类型的名称在不引人注目的客户端验证规则中必须是唯一的.不止一次看到以下验证类型:必需[英] Validation type names in unobtrusive client validation rules must be unique. The following validation type was seen more than once: required

本文是小编为大家收集整理的关于验证类型的名称在不引人注目的客户端验证规则中必须是唯一的.不止一次看到以下验证类型:必需的处理方法,想解了验证类型的名称在不引人注目的客户端验证规则中必须是唯一的.不止一次看到以下验证类型:必需的问题怎么解决?验证类型的名称在不引人注目的客户端验证规则中必须是唯一的.不止一次看到以下验证类型:必需问题的解决办法?那么可以参考本文帮助大家快速定位并解决问题。

问题描述

我创建了自定义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,则可以使用上述代码,但是客户端验证不起作用.

我的代码怎么了?

推荐答案

我找到了解决方案,我们必须在global.asax

中的application_start上添加以下代码
DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(LocalizedRequiredAttribute), typeof(RequiredAttributeAdapter));
DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(LocalizedNumericRegularExpressionAttribute), typeof(RegularExpressionAttributeAdapter));

其他推荐答案

您将验证类型的值与MVC自动验证相同.因此,您必须更改ModelClientValidationRule或其派生类中验证type ="名称唯一"的值.名称应避免MVC自动生成的名称,例如"日期","必需" ... 其他解决方案是通过将这些代码放在应用程序start

上关闭自动验证

dataannotationsmodelvalidatorprovider.addimpliticrequiredtributeforvaluetypes = false;

本文地址:https://www.itbaoku.cn/post/1793777.html