【问题标题】:Custom Validation in ASP.NET MVC doesn't work [duplicate]ASP.NET MVC 中的自定义验证不起作用 [重复]
【发布时间】:2016-04-01 12:02:51
【问题描述】:

我有两个执行模型验证的自定义验证。第一个是我做的一个控件,看字符串“”中是否有字符,第二个是看两个日期是否连续。

尖括号验证器

public class AngleBracketsValidator : ValidationAttribute
{    
    public override Boolean IsValid(Object value)
    {


        Boolean isValid = true;
        if (value != null && (value.ToString().Contains('<') || value.ToString().Contains('>')))
        {
            isValid = false;
        }

        return isValid;
    }
}

日期验证器

public class CustomDateCompareValidator : ValidationAttribute
{
    public String PropertyDateStartToCompare { get; set; }

    public String PropertyDateEndToCompare { get; set; }

    public CustomDateCompareValidator(string propertyDateStartToCompare, string propertyDateEndToCompare)
    {
        PropertyDateStartToCompare = propertyDateStartToCompare;
        PropertyDateEndToCompare = propertyDateEndToCompare;
    }

    public override Boolean IsValid(Object value)
    {
        Type objectType = value.GetType();
        PropertyInfo[] neededProperties =
          objectType.GetProperties()
          .Where(propertyInfo => propertyInfo.Name == PropertyDateStartToCompare || propertyInfo.Name == PropertyDateEndToCompare)
          .ToArray();

        if (neededProperties.Count() != 2)
        {
            throw new ApplicationException("CustomDateCompareValidator error on " + objectType.Name);
        }

        Boolean isValid = true;
        if (Convert.ToDateTime(neededProperties[0].GetValue(value, null)) != Convert.ToDateTime("01/01/0001") && Convert.ToDateTime(neededProperties[1].GetValue(value, null)) != Convert.ToDateTime("01/01/0001"))
        {
            if (Convert.ToDateTime(neededProperties[0].GetValue(value, null)) > Convert.ToDateTime(neededProperties[1].GetValue(value, null)))
            {
                isValid = false;
            }
        }    
        return isValid;
    }
}

型号:

[Serializable]
[CustomDateCompareValidator("DtStart", "DtEnd", ErrorMessage = "the start date is greater than that of the end.")]
public class ProjModel
{
    [Display(Name = "Codice:")]
    [AllowHtml]
    [AngleBracketsValidator(ErrorMessage = "Code can not contain angle bracket.")]
    public string Code { get; set; }

    [Display(Name = "Date Start:")]
    public DateTime? DtStart { get; set; }

    [Display(Name = "Date End:")]
    public DateTime? DtEnd { get; set; }

}

执行测试知道显示第一个验证器,即尖括号的验证器,而显示第二个验证器,即日期的验证器。但是,如果我在队列中发布公允价值,通过尖括号的验证,日期验证器查看会显示错误消息。 使其正常工作的一些想法?

【问题讨论】:

  • 请重新表述您的问题。很难理解实际上什么不起作用。
  • 抱歉我的英语不好...问题是我不能同时显示两个验证器。
  • 您没有将CustomDateCompareValidator 应用于课程。你将它应用到模型中的一个属性——比如DtEnd,然后你提供另一个属性(DtStart)来比较。建议您使用 foolproof [GreaterThan] 或类似的验证属性,这也将为您提供客户端验证。

标签: c# asp.net asp.net-mvc validation asp.net-mvc-4


【解决方案1】:

“不支持。如果任何属性级别验证失败,则不执行类级别验证”。 看原文entry

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多