从ASP.NET MVC 3中的RequiredAttribute加入时,客户端验证不起作用?

时间:2022-01-31 15:10:54

I created an inherited attribute like this in ASP.NET MVC3:

我在ASP.NET MVC3中创建了一个这样的继承属性:

public sealed class RequiredFromResourceAttribute : RequiredAttribute
{
    public RequiredFromResourceAttribute(string errorResourceName, string errorResourceTypeName)
    {
        this.ErrorMessageResourceName = errorResourceName;
        this.ErrorMessageResourceType = Type.GetType(errorResourceTypeName);
    }
}

And use it like this:

并像这样使用它:

[RequiredFromResource("Title", "Resources.Resource, MyProject.Mvc, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null")]
public string Title { get; set; }

It didn't work and the MVC ignored it. Then I create a simpler class which just inherited from RequiredAttribute like this:

它不起作用,MVC忽略了它。然后我创建一个更简单的类,它继承自RequiredAttribute,如下所示:

public class MyRequiredAttribute : RequiredAttribute
{
}

I use it like that I said. But it didn't work again.

我就像我说的那样使用它。但它没有再起作用。

Although, all these ways work on "DisplayNameAtrribute" perfectly.

尽管如此,所有这些方式都完美地适用于“DisplayNameAtrribute”。

What is the problem?

问题是什么?

2 个解决方案

#1


3  

You can fix this by adding the following code in Global.asax: (found the answer here)

您可以通过在Global.asax中添加以下代码来解决此问题:(在此处找到答案)

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

Alternatively, using marcind's solution, I found that the constructor for ModelClientValidationRequiredRule requires an error message. Here is an updated version that includes the display name for the field:

或者,使用marcind的解决方案,我发现ModelClientValidationRequiredRule的构造函数需要一条错误消息。这是一个更新版本,其中包含该字段的显示名称:

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

#2


3  

It's only client-side validation that does not work with inherited attributes. The reason for that is that MVC uses strict type equality when mapping server-side attributes to client validation behaviors.

它只是客户端验证,不适用于继承的属性。原因是MVC在将服务器端属性映射到客户端验证行为时使用严格类型相等。

To work around this you will need your custom attribute to implement IClientValidatable:

要解决此问题,您需要使用自定义属性来实现IClientValidatable:

public class MyRequiredAttribute : IClientValidatable {
    public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context) {
         yield return new ModelClientValidationRequiredRule();
    }
}

#1


3  

You can fix this by adding the following code in Global.asax: (found the answer here)

您可以通过在Global.asax中添加以下代码来解决此问题:(在此处找到答案)

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

Alternatively, using marcind's solution, I found that the constructor for ModelClientValidationRequiredRule requires an error message. Here is an updated version that includes the display name for the field:

或者,使用marcind的解决方案,我发现ModelClientValidationRequiredRule的构造函数需要一条错误消息。这是一个更新版本,其中包含该字段的显示名称:

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

#2


3  

It's only client-side validation that does not work with inherited attributes. The reason for that is that MVC uses strict type equality when mapping server-side attributes to client validation behaviors.

它只是客户端验证,不适用于继承的属性。原因是MVC在将服务器端属性映射到客户端验证行为时使用严格类型相等。

To work around this you will need your custom attribute to implement IClientValidatable:

要解决此问题,您需要使用自定义属性来实现IClientValidatable:

public class MyRequiredAttribute : IClientValidatable {
    public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context) {
         yield return new ModelClientValidationRequiredRule();
    }
}