C# 自定义验证器属性-在错误消息中使用其他属性值

C# 自定义验证器属性-在错误消息中使用其他属性值,c#,validation,asp.net-core-mvc,asp.net-core-1.1,C#,Validation,Asp.net Core Mvc,Asp.net Core 1.1,我有一个自定义属性,它依赖于其他模型属性来运行。 该属性使用属性名参数从关联模型中获取值以执行验证。 我将它附加到这样的字段 AViewModel{ [DateBetweenAges(minProperty:"MinAge", maxProperty:"MaxAge", ErrorMessage = "Your age is not between {1} and {2}")] public DateTime? DoB { get; set; } public

我有一个自定义属性,它依赖于其他模型属性来运行。 该属性使用属性名参数从关联模型中获取值以执行验证。 我将它附加到这样的字段

AViewModel{
    [DateBetweenAges(minProperty:"MinAge", maxProperty:"MaxAge", ErrorMessage = "Your age is not between {1} and {2}")]
    public DateTime? DoB { get; set; }

        public int MinAge { get; set; }
        public int MaxAge { get; set; }
}
我已经使用适配器和AdapterProvider将其连接起来,通过下面的示例提供客户端验证 但是我在获取错误消息时遇到了一些问题,例如:
“您的年龄不在20到30岁之间”

20和30是附加到ViewModel上的
MinAge
MaxAge
属性的值(在加载页面时设置)

在适配器中的GetErrorMessage(ModelValidationContextBase validationContext)方法中,我似乎根本无法获取ViewModel中的值

在属性本身的
IsValid
方法中,我通过执行反射来获得这些值

var maxprop = validationContext.ObjectType.GetProperty(MaxProperty);
var maxPropVal = maxProperty.GetValue(validationContext.ObjectInstance, null);
但是这些在适配器中似乎没有对象实例,尽管检查确实让我找到了包含它的ViewData

public class DateBetweenAgesAttributeAdapter : AttributeAdapterBase<DateBetweenAgesAttribute>
{
    private readonly DateBetweenAgesAttribute _attribute;

    public DateBetweenAgesAttributeAdapter(DateBetweenAgesAttribute attribute, IStringLocalizer localizer) : base(attribute, localizer)
    {
        _attribute = attribute;
    }

    public override void AddValidation(ClientModelValidationContext context)
    {
        MergeAttribute(context.Attributes, "data-val", "true");
        MergeAttribute(context.Attributes, "data-val-datebetweenages", GetErrorMessage(context));
        MergeAttribute(context.Attributes, "data-val-datebetweenages-min", _attribute.MinProperty);
        MergeAttribute(context.Attributes, "data-val-datebetweenages-max", _attribute.MaxProperty);
    }

    public override string GetErrorMessage(ModelValidationContextBase validationContext)
    {
return GetErrorMessage(validationContext.ModelMetadata,
            validationContext.ModelMetadata.GetDisplayName(),
            //would like to pass the actual minvalue in here,
            //would like to pass the actual maxvalue in here);
    }
..
公共类DateBetweenegateSattributeAdapter:AttributeAdapterBase
{
private readonly datebetweenegatesattribute _属性;
公共DateBetweenAgesAttributeAdapter(DateBetweenAgesAttribute属性,IStringLocalizer定位器):基(属性,定位器)
{
_属性=属性;
}
public override void AddValidation(ClientModelValidationContext上下文)
{
MergeAttribute(context.Attributes,“数据值”、“真”);
MergeAttribute(context.Attributes,“数据val datebetweenegs”,GetErrorMessage(context));
MergeAttribute(context.Attributes,“data val datebetweenages min”、_attribute.MinProperty);
MergeAttribute(context.Attributes,“data val DateBetweenegs max”、_attribute.MaxProperty);
}
公共重写字符串GetErrorMessage(ModelValidationContextBase validationContext)
{
返回GetErrorMessage(validationContext.ModelMetadata,
validationContext.ModelMetadata.GetDisplayName(),
//要在这里传递实际的最小值,
//要在此处传递实际的最大值);
}
..
我该怎么做


我意识到我可以为此使用[Remote]属性,也许我最终会这样做,但这似乎意味着复制内容。

我通过将属性的属性放入属性类而不是适配器的错误消息中来实现这一点,例如:

[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
public class DateBetweenAgesAttribute : ValidationAttribute {
    public int MinProperty { get; set; }
    public int MaxProperty { get; set; }

    public DateBetweenAgesAttribute(int min, int max) : base() {
        MinProperty = min;
        MaxProperty = max;
        ErrorMessage = $"Your age is not between {MinProperty} and {MaxProperty}";
    }
}

那么您就不需要在DateBetweeNageSatTributeTadapter.GetErrorMessage中执行任何操作,它只会按原样工作。

您是如何解决此问题的?