无法显示具有自定义验证属性的ErrorMessageResourceName

Arn*_*501 1 validation data-annotations asp.net-mvc-3

我基于这个问题,用asp.net mvc3和jquery validate创建自定义验证属性:

我已经更新它以使用一组字符串属性而不是bool.哪个工作正常.但是当我使用自定义ErrorMessage时,我的问题就出现了,不显示自定义消息.我无法弄清楚原因.

[RequiredOneFromGroup("PhoneGroup", 
    ErrorMessageResourceName = "PhoneGroup", 
    ErrorMessageResourceType = typeof(WizardStrings))]
public String Mobile { get; set; } 
[RequiredOneFromGroup("PhoneGroup", 
    ErrorMessageResourceName = "PhoneGroup", 
    ErrorMessageResourceType = typeof(WizardStrings))]
public String Phone { get; set; }
Run Code Online (Sandbox Code Playgroud)

这是自定义验证属性:

[AttributeUsage(AttributeTargets.Property)]
public class RequiredOneFromGroup : ValidationAttribute, IClientValidatable
{
    public RequiredOneFromGroup(string groupName)
    {
        ErrorMessage = string.Format("You must select at least one value from group \"{0}\"", groupName);
        GroupName = groupName;
    }

    public string GroupName { get; private set; }

    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        foreach (var property in GetGroupProperties(validationContext.ObjectType))
        {
            var propertyValue = (string)property.GetValue(validationContext.ObjectInstance, null);
            if ( ! string.IsNullOrWhiteSpace(propertyValue))
            {
                // at least one property is true in this group => the model is valid
                return null;
            }
        }
        return new ValidationResult(FormatErrorMessage(validationContext.DisplayName));
    }

    private IEnumerable<PropertyInfo> GetGroupProperties(Type type)
    {
        return
            from property in type.GetProperties()
            where property.PropertyType == typeof(string)
            let attributes = property.GetCustomAttributes(typeof(RequiredOneFromGroup), false).OfType<RequiredOneFromGroup>()
            where attributes.Count() > 0
            from attribute in attributes
            where attribute.GroupName == GroupName
            select property;
    }

    public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
    {
        var groupProperties = GetGroupProperties(metadata.ContainerType).Select(p => p.Name);
        var rule = new ModelClientValidationRule
        {
            ErrorMessage = this.ErrorMessage
        };
        rule.ValidationType = string.Format("group", GroupName.ToLower());
        rule.ValidationParameters["propertynames"] = string.Join(",", groupProperties);
        yield return rule;
    }
}
Run Code Online (Sandbox Code Playgroud)

我试图删除ErrorMessage的手动设置,但后来我会得到一个空的错误消息.如何检索我在模型的命名参数中指定的ErrorMessageResourceName的值?我如何设置它以便自定义验证属性显示它?

Dar*_*rov 5

在你的GetClientValidationRules方法中替换:

var rule = new ModelClientValidationRule
{
    ErrorMessage = this.ErrorMessage
};
Run Code Online (Sandbox Code Playgroud)

有:

var rule = new ModelClientValidationRule
{
    ErrorMessage = FormatErrorMessage(metadata.DisplayName)
};
Run Code Online (Sandbox Code Playgroud)

还要摆脱this.ErrorMessage = ...构造函数中的行.你不能同时设置的ErrorMessage和使用ErrorMessageResourceName,并ErrorMessageResourceType验证属性的属性.那些东西是相互排斥的.