コード例 #1
0
        public static Utils.Entity.EntityResult <TModel> Validate <TModel>(Form form)
            where TModel : class, new()
        {
            var controls             = form.GetControls();
            EntityResult <TModel> er = new EntityResult <TModel>();
            var    pis   = ExpressionReflector.GetProperties(typeof(TModel));
            TModel model = new TModel();

            foreach (Control item in controls.Values)
            {
                if (item is TextBox)
                {
                    var prefix       = GetControlPrefix <TextBox>();
                    var propertyPair = pis.Where(x => item.Name == prefix + x.Value.Name).FirstOrDefault();
                    if (string.IsNullOrEmpty(propertyPair.Key))
                    {
                        continue;
                    }
                    var  propertyInfo = propertyPair.Value;
                    Type propertyType = Nullable.GetUnderlyingType(propertyInfo.PropertyType);
                    if (propertyType == null)
                    {
                        propertyType = propertyInfo.PropertyType;
                    }
                    object value = item.Text;
                    value = Convert.ChangeType(value, propertyType);
                    var attrs = AttributeHelper.GetAttributes <ValidationAttribute>(propertyInfo);
                    foreach (var attr in attrs)
                    {
                        if (attr != null && !attr.IsValid(value))
                        {
                            er.Message = string.IsNullOrEmpty(attr.ErrorMessage) ? ("表单项" + item.Name + "验证失败") : attr.ErrorMessage;
                            return(er);
                        }
                    }
                    ExpressionReflector.SetValue(model, propertyPair.Key, value);
                }
                else if (item is NumericUpDown)
                {
                    var prefix       = GetControlPrefix <NumericUpDown>();
                    var propertyPair = pis.Where(x => item.Name == prefix + x.Value.Name).FirstOrDefault();
                    if (string.IsNullOrEmpty(propertyPair.Key))
                    {
                        continue;
                    }
                    var  propertyInfo = propertyPair.Value;
                    Type propertyType = Nullable.GetUnderlyingType(propertyInfo.PropertyType);
                    if (propertyType == null)
                    {
                        propertyType = propertyInfo.PropertyType;
                    }
                    object value = item.Text;
                    value = Convert.ChangeType(value, propertyType);
                    var attrs = AttributeHelper.GetAttributes <ValidationAttribute>(propertyInfo);
                    foreach (var attr in attrs)
                    {
                        if (attr != null && !attr.IsValid(value))
                        {
                            er.Message = string.IsNullOrEmpty(attr.ErrorMessage) ? ("表单项" + item.Name + "验证失败") : attr.ErrorMessage;
                            return(er);
                        }
                    }
                    ExpressionReflector.SetValue(model, propertyPair.Key, value);
                }
                else if (item is ComboBox)
                {
                    var prefix       = GetControlPrefix <ComboBox>();
                    var propertyPair = pis.Where(x => item.Name == prefix + x.Value.Name).FirstOrDefault();
                    if (string.IsNullOrEmpty(propertyPair.Key))
                    {
                        continue;
                    }
                    var  propertyInfo = propertyPair.Value;
                    Type propertyType = Nullable.GetUnderlyingType(propertyInfo.PropertyType);
                    if (propertyType == null)
                    {
                        propertyType = propertyInfo.PropertyType;
                    }
                    object value = item.Text;
                    value = Convert.ChangeType(value, propertyType);
                    var attrs = AttributeHelper.GetAttributes <ValidationAttribute>(propertyInfo);
                    foreach (var attr in attrs)
                    {
                        if (attr != null && !attr.IsValid(value))
                        {
                            er.Message = string.IsNullOrEmpty(attr.ErrorMessage) ? ("表单项" + item.Name + "验证失败") : attr.ErrorMessage;
                            return(er);
                        }
                    }
                    ExpressionReflector.SetValue(model, propertyPair.Key, value);
                }
            }
            er.Model   = model;
            er.Success = true;
            return(er);
        }