简体   繁体   English

不打扰的客户端验证规则中的验证类型名称必须唯一。 多次看到以下验证类型:必需

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

I created custom ASP.Net MVC model validation as the following: 我创建了以下自定义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
    }
}

the following is my Model: 以下是我的模型:

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

Whenever I navigate to form with above Model, the following exception has occurred. 每当我导航到具有上述模型的表单时,就会发生以下异常。

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

above codes are OK if I remove IClientValidatable , but the client validation doesn't work. 如果我删除IClientValidatable ,则以上代码可以,但是客户端验证无效。

What's wrong with my code? 我的代码有什么问题?

I found the solution, We have to add the following codes at Application_Start in global.asax 我找到了解决方法,我们必须在global.asax的Application_Start处添加以下代码

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

You put value of ValidationType same with MVC auto validate. 您将ValidationType的值与MVC自动验证相同。 So, you must change the value of ValidationType = "name unique" in ModelClientValidationRule or its derived class. 因此,您必须在ModelClientValidationRule或其派生类中更改ValidationType =“名称唯一”的值。 Name should avoid MVC auto generate name such as 'date', 'required'... Other solution is turn off auto validate by put these code on app start 名称应避免MVC自动生成名称,例如'date','required'...其他解决方案是通过将这些代码放到应用启动时关闭自动验证

DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false; DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 ASP.NET MVC 5 中出现以下错误:不显眼的客户端验证规则中的验证类型名称必须是唯一的。 所需 - Getting the following error in ASP.NET MVC 5: Validation type names in unobtrusive client validation rules must be unique. The […] required 不打扰的客户端验证规则必须唯一 - unobtrusive client validation rules must be unique 每个表中的列名必须唯一。 表“ HumanCustomers”中的列名“ LastName”已多次指定 - Column names in each table must be unique. Column name 'LastName' in table 'HumanCustomers' is specified more than once 客户端非侵入式验证 - client side unobtrusive validation 每个表中的列名必须唯一。 表Entity1&#39;中的列名&#39;Id&#39;被多次指定 - Column names in each table must be unique. Column name 'Id' in table Entity1' is specified more than once 每个表中的列名必须唯一。 表“ UserLogins”中的列名“ department_Id”已多次指定 - Column names in each table must be unique. Column name 'department_Id' in table 'UserLogins' is specified more than once 实体框架验证错误 - 类型中的每个属性名称必须是唯一的 - Entity Framework validation error - Each property name in a type must be unique 客户端自定义不显眼验证不起作用 - Client-Side Custom Unobtrusive Validation Not Working ValidationAttribute注入服务,用于不打扰的客户端验证 - ValidationAttribute injecting services for unobtrusive client validation 不接受模型的不打扰的客户端验证 - Unobtrusive Client Validation without accepting model
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM