1. 程式人生 > >aps.net手寫驗證模型的方法

aps.net手寫驗證模型的方法

    /// <summary>
    /// 基礎驗證類
    /// </summary>
    public class BaseValidator
    {
        /// <summary>
        /// 驗證輸入引數
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="requestModel"></param>
        /// <returns></returns>
public static ResponseModel BaseCheckParamModel<T>(T requestModel) where T : class { var context = new System.ComponentModel.DataAnnotations.ValidationContext(requestModel, null, null); var errors = new List<System.ComponentModel.DataAnnotations.ValidationResult>();
//通用校驗引數 var result = System.ComponentModel.DataAnnotations.Validator.TryValidateObject(requestModel, context, errors, true); if (!result) { return new ResponseModel(false, 1, errors.First().ErrorMessage); } return new
ResponseModel(true, 0, "success"); } }

但是有個問題,那就是,如果驗證的物件的某個屬性是物件,那麼這個子物件驗證不到。