using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Web;
using BLL.BLL;

namespace www.nabian.com.Models
{
    public class NameAndBearchValidation : ValidationAttribute
    {
        RegisterManagerBLL manager = new RegisterManagerBLL();
        public string OtherProperty { get; set; }
        public string OtherPropertyDisplayName
        {
            [CompilerGenerated]
            get
            {
                return OtherPropertyDisplayName;
            }
            [CompilerGenerated]
            internal set
            {
            }
        }
        public override bool RequiresValidationContext
        {
            get
            {
                return RequiresValidationContext;
            }
        }
        public NameAndBearchValidation(string otherProperty)
        {
            if (otherProperty == null)
                throw new ArgumentNullException("otherProperty");

            OtherProperty = otherProperty;
        }
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            var property = validationContext.ObjectType.GetProperty(OtherProperty);
            var other = property.GetValue(validationContext.ObjectInstance, null);
            if (manager.QueryComName(value.ToString()) <= 0 && manager.QueryComName(other.ToString(), value.ToString()) <= 0)
            {
                return ValidationResult.Success;
            }
            return ValidationResult.Success;
        }
    }
}

  测试

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using www.nabian.com.Models;

namespace www.nabian.com.ViewsModels
{
    public class textModel
    {
        [NameAndBearchValidation("comName", ErrorMessage = "两个属性不唯一")]
        public string name { get; set; }

        public string comName { get; set; }
    }
}

  

相关文章:

  • 2021-05-30
  • 2021-10-12
  • 2021-10-14
  • 2021-12-27
  • 2022-12-23
  • 2022-01-13
  • 2021-12-17
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-09-18
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-20
相关资源
相似解决方案