使用Linq-to-SQL和MVC进行OnFieldChanging验证

时间:2009-09-16 09:51:31

标签: asp.net-mvc linq-to-sql

我的部分类中有以下部分方法附加到我的Liq to SQL模型中的对象。

每种方法中的第一次验证检查适用于检查每种方法中的有效条目。但是,第二个if语句用于检查用户是否已设置至少一个字段,但是当使用将这些字段留空时,实际上从不调用这些方法。我在DataAnnotation标签中有一些验证,但我不能使用必需的属性,因为它是一个“Either or”场景。

有没有办法坚持我目前的验证模式并在模型中进行这种验证? (备份计划是在控制器中对其进行编码)

partial void OnTelChanging(string value)
        {
            if (!PhoneValidator.IsValidNumber(value))
                _errors.Add("Tel", "Telephone No must be a valid UK phone number.");


            if (String.IsNullOrEmpty(this.Mob) && String.IsNullOrEmpty(this.Tel))
            {
                _errors.Add("Tel", "You must enter at least one contact number.");
                _errors.Add("Mob", "");
            }
        }

        partial void OnMobChanging(string value)
        {
            if (!PhoneValidator.IsValidNumber(value))
                _errors.Add("Mob", "Mobile must be a valid UK phone number.");


            if (String.IsNullOrEmpty(this.Mob) && String.IsNullOrEmpty(this.Tel))
            {
                _errors.Add("Tel", "You must enter at least one contact number.");
                _errors.Add("Mob", "");
            }
        }





 [Bind(Include = "EthnicOriginID,CreatedByUserID,AddressID,CarefirstNumber,Title,Forename,Surname,Dob,Tel,Mob,GuardianName,Notes,Archived")]
    [MetadataType(typeof(CustomerMetaData))]
    public partial class Customer : IDataErrorInfo
    {
        private Dictionary<string, string> _errors = new Dictionary<string, string>();

        partial void OnTelChanging(string value)
        {
            if (!String.IsNullOrEmpty(value) && !PhoneValidator.IsValidNumber(value))
                _errors.Add("Tel", "Telephone No must be a valid UK phone number.");


            if (String.IsNullOrEmpty(this.Mob) && String.IsNullOrEmpty(this.Tel))
            {
                _errors.Add("Tel", "You must enter at least one contact number.");
                _errors.Add("Mob", "");
            }
        }

        partial void OnMobChanging(string value)
        {
            if (!String.IsNullOrEmpty(value) && !PhoneValidator.IsValidNumber(value))
                _errors.Add("Mob", "Mobile must be a valid UK phone number.");


            if (String.IsNullOrEmpty(this.Mob) && String.IsNullOrEmpty(this.Tel))
            {
                _errors.Add("Tel", "You must enter at least one contact number.");
                _errors.Add("Mob", "");
            }
        }

        #region ToString Override

        /// <summary>
        /// Returns a <see cref="System.String"/> that represents this instance.
        /// </summary>
        /// <returns>
        /// A <see cref="System.String"/> that represents this instance.
        /// </returns>
        public override string ToString()
        {
            return this.Surname + ", " + this.Forename;
        }

        #endregion ToString Override

        #region IDataErrorInfo Members

        public string Error
        {
            get
            {
                return string.Empty;
            }
        }

        public string this[string columnName]
        {
            get
            {
                if (_errors.ContainsKey(columnName))
                    return _errors[columnName];
                return string.Empty;
            }
        }

        #endregion
    }

    public class CustomerMetaData
    {
        #region EthnicOriginID

        [DisplayName("Ethnic Origin")]
        public int EthnicOriginID { get; set; }

        #endregion EthnicOriginID

        #region CreatedByUserID

        [DisplayName("Created By User")]
        [Required]
        public int CreatedByUserID { get; set; }

        #endregion CreatedByUserID

        #region AddressID

        [DisplayName("Address")]
        public int AddressID { get; set; }

        #endregion AddressID

        #region CarefirstNumber

        [DataType(DataType.Text)]
        [DisplayName("Carefirst Number")]
        [StringLength(8)]
        public string CarefirstNumber { get; set; }

        #endregion CarefirstNumber

        #region Title

        [DataType(DataType.Text)]
        [DisplayName("Title")]
        [StringLength(10)]
        public string Title { get; set; }

        #endregion Title

        #region Forename

        [DataType(DataType.Text)]
        [DisplayName("Forename")]
        [StringLength(25)]
        [Required]
        public string Forename { get; set; }

        #endregion Forename

        #region Surname

        [DataType(DataType.Text)]
        [DisplayName("Surname")]
        [StringLength(25)]
        [Required]
        public string Surname { get; set; }

        #endregion Surname

        #region Dob

        [DataType(DataType.DateTime)]
        [DisplayName("Dob")]
        public DateTime Dob { get; set; }

        #endregion Dob

        #region Tel

        [DataType(DataType.Text)]
        [DisplayName("Telephone No.")]
        [StringLength(11)]
        public string Tel { get; set; }

        #endregion Tel

        #region Mob

        [DataType(DataType.Text)]
        [DisplayName("Mobile")]
        [StringLength(11)]
        public string Mob { get; set; }

        #endregion Mob

        #region GuardianName

        [DataType(DataType.Text)]
        [DisplayName("Guardian Name")]
        [StringLength(50)]
        public string GuardianName { get; set; }

        #endregion GuardianName

        #region Notes

        [DataType(DataType.Text)]
        [DisplayName("Notes")]
        [StringLength(300)]
        public string Notes { get; set; }

        #endregion Notes
    }

2 个答案:

答案 0 :(得分:2)

Implement IDataErrorInfo。这将做你想要的,MVC会自动选择它。

答案 1 :(得分:1)

这些属性的onChange事件永远不会触发。在使用服务层进行验证时,您应该从ASP.NET查看此tutorial

您可以将服务层与数据注释结合使用,以用于任何复杂的验证规则。