Simplify CompositeModelBinder
Also adds a diagnostic ToString to ModelBindingResult to make testing easier.
This commit is contained in:
parent
382c9ab86a
commit
ab08e27a4b
|
|
@ -76,20 +76,6 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
|
|||
return Task.FromResult(Success(key, model, validationNode));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="ModelBindingResult"/> using the provided <paramref name="key"/>.
|
||||
/// </summary>
|
||||
/// <param name="key">The key of the current model binding operation.</param>
|
||||
/// <param name="other">The other <see cref="ModelBindingResult" /> to copy from.</param>
|
||||
public ModelBindingResult([NotNull] string key, ModelBindingResult other)
|
||||
{
|
||||
Key = key;
|
||||
|
||||
Model = other.Model;
|
||||
IsModelSet = other.IsModelSet;
|
||||
ValidationNode = other.ValidationNode;
|
||||
}
|
||||
|
||||
private ModelBindingResult(string key, object model, bool isModelSet, ModelValidationNode validationNode)
|
||||
{
|
||||
Key = key;
|
||||
|
|
@ -171,7 +157,7 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
|
|||
}
|
||||
else if (IsModelSet)
|
||||
{
|
||||
return $"Success {Key} -> {Model}";
|
||||
return $"Success {Key} -> '{Model}'";
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
|
|||
|
|
@ -32,51 +32,16 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
|
|||
/// <inheritdoc />
|
||||
public IReadOnlyList<IModelBinder> ModelBinders { get; }
|
||||
|
||||
public virtual async Task<ModelBindingResult> BindModelAsync([NotNull] ModelBindingContext bindingContext)
|
||||
public virtual Task<ModelBindingResult> BindModelAsync([NotNull] ModelBindingContext bindingContext)
|
||||
{
|
||||
var newBindingContext = CreateNewBindingContext(bindingContext);
|
||||
if (newBindingContext == null)
|
||||
{
|
||||
// Unable to find a value provider for this binding source. Binding will fail.
|
||||
return ModelBindingResult.NoResult;
|
||||
return ModelBindingResult.NoResultAsync;
|
||||
}
|
||||
|
||||
var modelBindingResult = await RunModelBinders(newBindingContext);
|
||||
if (modelBindingResult == ModelBindingResult.NoResult)
|
||||
{
|
||||
// Unable to bind or something went wrong.
|
||||
return ModelBindingResult.NoResult;
|
||||
}
|
||||
|
||||
var bindingKey = bindingContext.ModelName;
|
||||
if (modelBindingResult.IsModelSet)
|
||||
{
|
||||
// Update the model state key if we are bound using an empty prefix and it is a complex type.
|
||||
// This is needed as validation uses the model state key to log errors. The client validation expects
|
||||
// the errors with property names rather than the full name.
|
||||
if (newBindingContext.ModelMetadata.IsComplexType && string.IsNullOrEmpty(modelBindingResult.Key))
|
||||
{
|
||||
// For non-complex types, if we fell back to the empty prefix, we should still be using the name
|
||||
// of the parameter/property. Complex types have their own property names which acts as model
|
||||
// state keys and do not need special treatment.
|
||||
// For example :
|
||||
//
|
||||
// public class Model
|
||||
// {
|
||||
// public int SimpleType { get; set; }
|
||||
// }
|
||||
// public void Action(int id, Model model)
|
||||
// {
|
||||
// }
|
||||
//
|
||||
// In this case, for the model parameter the key would be SimpleType instead of model.SimpleType.
|
||||
// (i.e here the prefix for the model key is empty).
|
||||
// For the id parameter the key would be id.
|
||||
bindingKey = string.Empty;
|
||||
}
|
||||
}
|
||||
|
||||
return new ModelBindingResult(bindingKey, modelBindingResult);
|
||||
return RunModelBinders(newBindingContext);
|
||||
}
|
||||
|
||||
private async Task<ModelBindingResult> RunModelBinders(ModelBindingContext bindingContext)
|
||||
|
|
|
|||
Loading…
Reference in New Issue