e79aa3c0ed
Former-commit-id: a2155e9bd80020e49e72e86c44da02a8ac0e57a4
42 lines
1.6 KiB
C#
42 lines
1.6 KiB
C#
namespace System.Web.ModelBinding {
|
|
using System;
|
|
|
|
public class DefaultModelBinder : IModelBinder {
|
|
|
|
public DefaultModelBinder() {
|
|
Providers = ModelBinderProviders.Providers;
|
|
}
|
|
|
|
public ModelBinderProviderCollection Providers {
|
|
get;
|
|
private set;
|
|
}
|
|
|
|
public bool BindModel(ModelBindingExecutionContext modelBindingExecutionContext, ModelBindingContext bindingContext) {
|
|
ModelBindingContext newBindingContext = bindingContext;
|
|
IModelBinder binder = Providers.GetBinder(modelBindingExecutionContext, bindingContext);
|
|
if (binder == null && !String.IsNullOrEmpty(bindingContext.ModelName)
|
|
&& bindingContext.ModelMetadata.IsComplexType) {
|
|
|
|
// fallback to empty prefix?
|
|
newBindingContext = new ModelBindingContext(bindingContext) {
|
|
ModelName = String.Empty,
|
|
ModelMetadata = bindingContext.ModelMetadata
|
|
};
|
|
binder = Providers.GetBinder(modelBindingExecutionContext, newBindingContext);
|
|
}
|
|
|
|
if (binder != null) {
|
|
bool boundSuccessfully = binder.BindModel(modelBindingExecutionContext, newBindingContext);
|
|
if (boundSuccessfully) {
|
|
// run validation
|
|
newBindingContext.ValidationNode.Validate(modelBindingExecutionContext, parentNode:null);
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false; // something went wrong
|
|
}
|
|
}
|
|
}
|