首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >System.ComponentModel.dataAnnotations.CustomValidation

System.ComponentModel.dataAnnotations.CustomValidation
EN

Stack Overflow用户
提问于 2010-10-21 18:58:27
回答 1查看 415关注 0票数 0

有没有人能帮我做一下System.ComponentModel.dataAnnotations.CustomValidation.的全面展示

这是我的头像

我有EventMetadata & EventAttributeMetadata课程。在EventMetadata中,我有Startdate和Enddate属性&在EventAttributeMetadata中,我有HoldOutdate属性。我想对HoldOutdate属性执行以下验证。“延迟日期应在事件开始日期和结束日期之间”。这应该使用System.ComponentModel.dataAnnotations命名空间。

EN

回答 1

Stack Overflow用户

发布于 2010-10-21 19:57:54

此示例位于创建一个非空的默认mvc应用程序时创建的AccountModel中,并根据需要对其进行修改和使用。

代码语言:javascript
复制
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = true)]
public class PropertiesMustMatchAttribute : ValidationAttribute
{
    private const string _defaultErrorMessage = "'{0}' and '{1}' do not match.";
    private readonly object _typeId = new object();

    public PropertiesMustMatchAttribute(string originalProperty, string confirmProperty)
        : base(_defaultErrorMessage)
    {
        OriginalProperty = originalProperty;
        ConfirmProperty = confirmProperty;
    }

    public string ConfirmProperty { get; private set; }
    public string OriginalProperty { get; private set; }

    public override object TypeId
    {
        get
        {
            return _typeId;
        }
    }

    public override string FormatErrorMessage(string name)
    {
        return String.Format(CultureInfo.CurrentUICulture, ErrorMessageString,
            OriginalProperty, ConfirmProperty);
    }

    public override bool IsValid(object value)
    {
        PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(value);
        object originalValue = properties.Find(OriginalProperty, true /* ignoreCase */).GetValue(value);
        object confirmValue = properties.Find(ConfirmProperty, true /* ignoreCase */).GetValue(value);
        return Object.Equals(originalValue, confirmValue);
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3986689

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档