Example #1
0
 public LoginViewModel()
 {
     logindata = DynamicModelBuilder.GetInstance <ILoginVM>(typeof(BaseModel));
     logindata.ValidateMetadataType = typeof(LoginVM);
 }
Example #2
0
 public UserDetailViewModel(bool needValidate = true) : base(needValidate)
 {
     userdata = DynamicModelBuilder.GetInstance <IUserVM>(parent: typeof(BaseModel), ctorArgs: needValidate);
     userdata.ValidateMetadataType = typeof(UserVM);
 }
Example #3
0
 public PermDetailViewModel(bool needValidate = true) : base(needValidate)
 {
     permdata = DynamicModelBuilder.GetInstance <IPermVM>(parent: typeof(BaseModel), ctorArgs: needValidate);
     permdata.ValidateMetadataType = typeof(PermVM);
 }
Example #4
0
 public GroupDetailViewModel(bool needValidate = true) : base(needValidate)
 {
     groupdata = DynamicModelBuilder.GetInstance <IGroupVM>(parent: typeof(BaseModel), ctorArgs: needValidate);
     groupdata.ValidateMetadataType = typeof(GroupVM);
 }
Example #5
0
        public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext)
        {
            if (!CanBindType(bindingContext.ModelType))
            {
                return(false);
            }
            if (!bindingContext.ValueProvider.ContainsPrefix(bindingContext.ModelName))
            {
                return(false);
            }
            //创建针对目标类型的空对象
            var ins = DynamicModelBuilder.GetInstance <IContact>(parent: bindingContext.ModelType, propAttrProvider: () =>
            {
                return(new List <PropertyCustomAttributeUnit>
                {
                    new PropertyCustomAttributeUnit
                    {
                        prop_name = "phone",
                        attrs = new List <PropertyCustomAttribute>
                        {
                            new PropertyCustomAttribute
                            {
                                attr_type = "System.ComponentModel.DataAnnotations.RequiredAttribute,System.ComponentModel.DataAnnotations, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35",
                                ctor_arg_types = "",
                                ctor_arg_values = new object[0],
                                error_msg = "姓名不能为空",
                                error_msg_res_name = "",
                                error_msg_res_type = ""
                            }
                        }
                    }
                });
            });

            //bindingContext.Model = Activator.CreateInstance(bindingContext.ModelType);
            bindingContext.Model = ins;

            //创建针对目标类型的ComplexModelDto
            //创建ModelBindingContext,并将ComplexModelDto作为Model,驱动新一轮的Model绑定
            ComplexModelDto     dto        = new ComplexModelDto(bindingContext.ModelMetadata, bindingContext.PropertyMetadata.Values);
            ModelBindingContext subContext = new ModelBindingContext(bindingContext)
            {
                ModelMetadata = actionContext.GetMetadataProvider().GetMetadataForType(() => dto, typeof(ComplexModelDto)),
                ModelName     = bindingContext.ModelName
            };

            actionContext.Bind(subContext);

            //从ComplexModelDto获取相应的值并为相应的属性赋值
            foreach (KeyValuePair <ModelMetadata, ComplexModelDtoResult> item in dto.Results)
            {
                ModelMetadata         propertyMetadata = item.Key;
                ComplexModelDtoResult dtoResult        = item.Value;
                if (dtoResult != null)
                {
                    PropertyInfo propertyInfo = bindingContext.ModelType.GetProperty(propertyMetadata.PropertyName);
                    if (propertyInfo.CanWrite)
                    {
                        propertyInfo.SetValue(bindingContext.Model, dtoResult.Model);
                    }
                }
            }
            return(true);
        }
Example #6
0
 public ChgPwdDetailViewModel(bool needValidate = true) : base(needValidate)
 {
     chgpwddata = DynamicModelBuilder.GetInstance <IChgPwdVM>(parent: typeof(BaseModel), ctorArgs: needValidate);
     chgpwddata.ValidateMetadataType = typeof(ChgPwdVM);
 }