Example #1
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 #2
0
 private bool TryBind(HttpActionContext actionContext, ModelBindingContext bindingContext)
 {
     return(actionContext.Bind(bindingContext, Binders));
 }