public bool BindModel(ModelBindingExecutionContext modelBindingExecutionContext, ModelBindingContext bindingContext) { ModelBinderUtil.ValidateBindingContext(bindingContext, typeof(ComplexModel), false /* allowNullModel */); ComplexModel complexModel = (ComplexModel)bindingContext.Model; foreach (ModelMetadata propertyMetadata in complexModel.PropertyMetadata) { ModelBindingContext propertyBindingContext = new ModelBindingContext(bindingContext) { ModelMetadata = propertyMetadata, ModelName = ModelBinderUtil.CreatePropertyModelName(bindingContext.ModelName, propertyMetadata.PropertyName) }; // bind and propagate the values IModelBinder propertyBinder = bindingContext.ModelBinderProviders.GetBinder(modelBindingExecutionContext, propertyBindingContext); if (propertyBinder != null) { if (propertyBinder.BindModel(modelBindingExecutionContext, propertyBindingContext)) { complexModel.Results[propertyMetadata] = new ComplexModelResult(propertyBindingContext.Model, propertyBindingContext.ValidationNode); } else { complexModel.Results[propertyMetadata] = null; } } } return(true); }
public void CreatePropertyModelName_EmptyParentName() { // Act string fullChildName = ModelBinderUtil.CreatePropertyModelName("", "childName"); // Assert Assert.Equal("childName", fullChildName); }
public static bool TryBindStrongModel <TModel>(ModelBindingExecutionContext modelBindingExecutionContext, ModelBindingContext parentBindingContext, string propertyName, ModelMetadataProvider metadataProvider, out TModel model) { ModelBindingContext propertyBindingContext = new ModelBindingContext(parentBindingContext) { ModelMetadata = metadataProvider.GetMetadataForType(null, typeof(TModel)), ModelName = ModelBinderUtil.CreatePropertyModelName(parentBindingContext.ModelName, propertyName) }; IModelBinder binder = parentBindingContext.ModelBinderProviders.GetBinder(modelBindingExecutionContext, propertyBindingContext); if (binder != null) { if (binder.BindModel(modelBindingExecutionContext, propertyBindingContext)) { object untypedModel = propertyBindingContext.Model; model = ModelBinderUtil.CastOrDefault <TModel>(untypedModel); parentBindingContext.ValidationNode.ChildNodes.Add(propertyBindingContext.ValidationNode); return(true); } } model = default(TModel); return(false); }