Remove Key from ModelBindingResult

This commit is contained in:
Ryan Nowak 2016-05-20 11:07:46 -07:00
parent ef29749e72
commit 88c9ae6588
38 changed files with 136 additions and 238 deletions

View File

@ -2,7 +2,6 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System; using System;
using System.Threading.Tasks;
using Microsoft.Extensions.Internal; using Microsoft.Extensions.Internal;
namespace Microsoft.AspNetCore.Mvc.ModelBinding namespace Microsoft.AspNetCore.Mvc.ModelBinding
@ -15,39 +14,24 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding
/// <summary> /// <summary>
/// Creates a <see cref="ModelBindingResult"/> representing a failed model binding operation. /// Creates a <see cref="ModelBindingResult"/> representing a failed model binding operation.
/// </summary> /// </summary>
/// <param name="key">The key of the current model binding operation.</param>
/// <returns>A <see cref="ModelBindingResult"/> representing a failed model binding operation.</returns> /// <returns>A <see cref="ModelBindingResult"/> representing a failed model binding operation.</returns>
public static ModelBindingResult Failed(string key) public static ModelBindingResult Failed()
{ {
if (key == null) return new ModelBindingResult(model: null, isModelSet: false);
{
throw new ArgumentNullException(nameof(key));
}
return new ModelBindingResult(key, model: null, isModelSet: false);
} }
/// <summary> /// <summary>
/// Creates a <see cref="ModelBindingResult"/> representing a successful model binding operation. /// Creates a <see cref="ModelBindingResult"/> representing a successful model binding operation.
/// </summary> /// </summary>
/// <param name="key">The key of the current model binding operation.</param>
/// <param name="model">The model value. May be <c>null.</c></param> /// <param name="model">The model value. May be <c>null.</c></param>
/// <returns>A <see cref="ModelBindingResult"/> representing a successful model bind.</returns> /// <returns>A <see cref="ModelBindingResult"/> representing a successful model bind.</returns>
public static ModelBindingResult Success( public static ModelBindingResult Success(object model)
string key,
object model)
{ {
if (key == null) return new ModelBindingResult( model, isModelSet: true);
{
throw new ArgumentNullException(nameof(key));
}
return new ModelBindingResult(key, model, isModelSet: true);
} }
private ModelBindingResult(string key, object model, bool isModelSet) private ModelBindingResult(object model, bool isModelSet)
{ {
Key = key;
Model = model; Model = model;
IsModelSet = isModelSet; IsModelSet = isModelSet;
} }
@ -57,16 +41,6 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding
/// </summary> /// </summary>
public object Model { get; } public object Model { get; }
/// <summary>
/// <para>
/// Gets the model name which was used to bind the model.
/// </para>
/// <para>
/// This property can be used during validation to add model state for a bound model.
/// </para>
/// </summary>
public string Key { get; }
/// <summary> /// <summary>
/// <para> /// <para>
/// Gets a value indicating whether or not the <see cref="Model"/> value has been set. /// Gets a value indicating whether or not the <see cref="Model"/> value has been set.
@ -96,7 +70,6 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding
public override int GetHashCode() public override int GetHashCode()
{ {
var hashCodeCombiner = HashCodeCombiner.Start(); var hashCodeCombiner = HashCodeCombiner.Start();
hashCodeCombiner.Add(Key, StringComparer.OrdinalIgnoreCase);
hashCodeCombiner.Add(IsModelSet); hashCodeCombiner.Add(IsModelSet);
hashCodeCombiner.Add(Model); hashCodeCombiner.Add(Model);
@ -107,7 +80,6 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding
public bool Equals(ModelBindingResult other) public bool Equals(ModelBindingResult other)
{ {
return return
string.Equals(Key, other.Key, StringComparison.OrdinalIgnoreCase) &&
IsModelSet == other.IsModelSet && IsModelSet == other.IsModelSet &&
object.Equals(Model, other.Model); object.Equals(Model, other.Model);
} }
@ -115,17 +87,13 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding
/// <inheritdoc /> /// <inheritdoc />
public override string ToString() public override string ToString()
{ {
if (Key == null) if (IsModelSet)
{ {
return "No Result"; return $"Success '{Model}'";
}
else if (IsModelSet)
{
return $"Success {Key} -> '{Model}'";
} }
else else
{ {
return $"Failed {Key}"; return $"Failed";
} }
} }

View File

@ -194,7 +194,7 @@ namespace Microsoft.AspNetCore.Mvc.Internal
_validator.Validate( _validator.Validate(
controllerContext, controllerContext,
modelBindingContext.ValidationState, modelBindingContext.ValidationState,
modelBindingResult.Value.Key, modelBindingContext.ModelName,
modelBindingResult.Value.Model); modelBindingResult.Value.Model);
} }

View File

@ -185,11 +185,6 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding
} }
set set
{ {
if (value.HasValue && value.Value == default(ModelBindingResult))
{
throw new ArgumentException(nameof(ModelBindingResult));
}
_state.Result = value; _state.Result = value;
} }
} }

View File

@ -12,7 +12,7 @@ namespace Microsoft.AspNetCore.Mvc.Internal
public Task BindModelAsync(ModelBindingContext bindingContext) public Task BindModelAsync(ModelBindingContext bindingContext)
{ {
bindingContext.Result = ModelBindingResult.Failed(bindingContext.ModelName); bindingContext.Result = ModelBindingResult.Failed();
return TaskCache.CompletedTask; return TaskCache.CompletedTask;
} }
} }

View File

@ -55,7 +55,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
if (bindingContext.Result == null) if (bindingContext.Result == null)
{ {
bindingContext.Result = ModelBindingResult.Failed(bindingContext.ModelName); bindingContext.Result = ModelBindingResult.Failed();
} }
} }
} }

View File

@ -95,7 +95,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
// This model binder is the only handler for the Body binding source and it cannot run twice. Always // This model binder is the only handler for the Body binding source and it cannot run twice. Always
// tell the model binding system to skip other model binders and never to fall back i.e. indicate a // tell the model binding system to skip other model binders and never to fall back i.e. indicate a
// fatal error. // fatal error.
bindingContext.Result = ModelBindingResult.Failed(modelBindingKey); bindingContext.Result = ModelBindingResult.Failed();
return; return;
} }
@ -109,11 +109,11 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
{ {
// Formatter encountered an error. Do not use the model it returned. As above, tell the model // Formatter encountered an error. Do not use the model it returned. As above, tell the model
// binding system to skip other model binders and never to fall back. // binding system to skip other model binders and never to fall back.
bindingContext.Result = ModelBindingResult.Failed(modelBindingKey); bindingContext.Result = ModelBindingResult.Failed();
return; return;
} }
bindingContext.Result = ModelBindingResult.Success(modelBindingKey, model); bindingContext.Result = ModelBindingResult.Success(model);
return; return;
} }
catch (Exception ex) catch (Exception ex)
@ -123,7 +123,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
// This model binder is the only handler for the Body binding source and it cannot run twice. Always // This model binder is the only handler for the Body binding source and it cannot run twice. Always
// tell the model binding system to skip other model binders and never to fall back i.e. indicate a // tell the model binding system to skip other model binders and never to fall back i.e. indicate a
// fatal error. // fatal error.
bindingContext.Result = ModelBindingResult.Failed(modelBindingKey); bindingContext.Result = ModelBindingResult.Failed();
return; return;
} }
} }

View File

@ -24,7 +24,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
var valueProviderResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName); var valueProviderResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
if (valueProviderResult == ValueProviderResult.None) if (valueProviderResult == ValueProviderResult.None)
{ {
bindingContext.Result = ModelBindingResult.Failed(bindingContext.ModelName); bindingContext.Result = ModelBindingResult.Failed();
return TaskCache.CompletedTask; return TaskCache.CompletedTask;
} }
@ -34,14 +34,14 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
var value = valueProviderResult.FirstValue; var value = valueProviderResult.FirstValue;
if (string.IsNullOrEmpty(value)) if (string.IsNullOrEmpty(value))
{ {
bindingContext.Result = ModelBindingResult.Failed(bindingContext.ModelName); bindingContext.Result = ModelBindingResult.Failed();
return TaskCache.CompletedTask; return TaskCache.CompletedTask;
} }
try try
{ {
var model = Convert.FromBase64String(value); var model = Convert.FromBase64String(value);
bindingContext.Result = ModelBindingResult.Success(bindingContext.ModelName, model); bindingContext.Result = ModelBindingResult.Success(model);
return TaskCache.CompletedTask; return TaskCache.CompletedTask;
} }
catch (Exception exception) catch (Exception exception)
@ -54,7 +54,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
// Matched the type (byte[]) only this binder supports. As in missing data cases, always tell the model // Matched the type (byte[]) only this binder supports. As in missing data cases, always tell the model
// binding system to skip other model binders i.e. return non-null. // binding system to skip other model binders i.e. return non-null.
bindingContext.Result = ModelBindingResult.Failed(bindingContext.ModelName); bindingContext.Result = ModelBindingResult.Failed();
return TaskCache.CompletedTask; return TaskCache.CompletedTask;
} }
} }

View File

@ -28,7 +28,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
// DO NOT simplify this code by removing the cast. // DO NOT simplify this code by removing the cast.
var model = (object)bindingContext.HttpContext.RequestAborted; var model = (object)bindingContext.HttpContext.RequestAborted;
bindingContext.ValidationState.Add(model, new ValidationStateEntry() { SuppressValidation = true }); bindingContext.ValidationState.Add(model, new ValidationStateEntry() { SuppressValidation = true });
bindingContext.Result = ModelBindingResult.Success(bindingContext.ModelName, model); bindingContext.Result = ModelBindingResult.Success(model);
return TaskCache.CompletedTask; return TaskCache.CompletedTask;
} }

View File

@ -63,7 +63,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
model = CreateEmptyCollection(bindingContext.ModelType); model = CreateEmptyCollection(bindingContext.ModelType);
} }
bindingContext.Result = ModelBindingResult.Success(bindingContext.ModelName, model); bindingContext.Result = ModelBindingResult.Success(model);
} }
return; return;
@ -110,7 +110,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
valueProviderResult); valueProviderResult);
} }
bindingContext.Result = ModelBindingResult.Success(bindingContext.ModelName, model); bindingContext.Result = ModelBindingResult.Success(model);
} }
/// <inheritdoc /> /// <inheritdoc />

View File

@ -90,12 +90,12 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
model: propertyModel)) model: propertyModel))
{ {
await BindProperty(bindingContext); await BindProperty(bindingContext);
result = bindingContext.Result ?? ModelBindingResult.Failed(modelName); result = bindingContext.Result ?? ModelBindingResult.Failed();
} }
if (result.IsModelSet) if (result.IsModelSet)
{ {
SetProperty(bindingContext, property, result); SetProperty(bindingContext, modelName, property, result);
} }
else if (property.IsBindingRequired) else if (property.IsBindingRequired)
{ {
@ -104,7 +104,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
} }
} }
bindingContext.Result = ModelBindingResult.Success(bindingContext.ModelName, bindingContext.Model); bindingContext.Result = ModelBindingResult.Success(bindingContext.Model);
} }
/// <summary> /// <summary>
@ -335,10 +335,12 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
/// Updates a property in the current <see cref="ModelBindingContext.Model"/>. /// Updates a property in the current <see cref="ModelBindingContext.Model"/>.
/// </summary> /// </summary>
/// <param name="bindingContext">The <see cref="ModelBindingContext"/>.</param> /// <param name="bindingContext">The <see cref="ModelBindingContext"/>.</param>
/// <param name="modelName">The model name.</param>
/// <param name="propertyMetadata">The <see cref="ModelMetadata"/> for the property to set.</param> /// <param name="propertyMetadata">The <see cref="ModelMetadata"/> for the property to set.</param>
/// <param name="result">The <see cref="ModelBindingResult"/> for the property's new value.</param> /// <param name="result">The <see cref="ModelBindingResult"/> for the property's new value.</param>
protected virtual void SetProperty( protected virtual void SetProperty(
ModelBindingContext bindingContext, ModelBindingContext bindingContext,
string modelName,
ModelMetadata propertyMetadata, ModelMetadata propertyMetadata,
ModelBindingResult result) ModelBindingResult result)
{ {
@ -347,6 +349,11 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
throw new ArgumentNullException(nameof(bindingContext)); throw new ArgumentNullException(nameof(bindingContext));
} }
if (modelName == null)
{
throw new ArgumentNullException(nameof(modelName));
}
if (propertyMetadata == null) if (propertyMetadata == null)
{ {
throw new ArgumentNullException(nameof(propertyMetadata)); throw new ArgumentNullException(nameof(propertyMetadata));
@ -372,12 +379,13 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
} }
catch (Exception exception) catch (Exception exception)
{ {
AddModelError(exception, bindingContext, result); AddModelError(exception, modelName, bindingContext, result);
} }
} }
private static void AddModelError( private static void AddModelError(
Exception exception, Exception exception,
string modelName,
ModelBindingContext bindingContext, ModelBindingContext bindingContext,
ModelBindingResult result) ModelBindingResult result)
{ {
@ -389,11 +397,10 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
// Do not add an error message if a binding error has already occurred for this property. // Do not add an error message if a binding error has already occurred for this property.
var modelState = bindingContext.ModelState; var modelState = bindingContext.ModelState;
var modelStateKey = result.Key; var validationState = modelState.GetFieldValidationState(modelName);
var validationState = modelState.GetFieldValidationState(modelStateKey);
if (validationState == ModelValidationState.Unvalidated) if (validationState == ModelValidationState.Unvalidated)
{ {
modelState.AddModelError(modelStateKey, exception, bindingContext.ModelMetadata); modelState.AddModelError(modelName, exception, bindingContext.ModelMetadata);
} }
} }
} }

View File

@ -37,7 +37,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
model = new EmptyFormCollection(); model = new EmptyFormCollection();
} }
bindingContext.Result = ModelBindingResult.Success(bindingContext.ModelName, model); bindingContext.Result = ModelBindingResult.Success(model);
} }
private class EmptyFormCollection : IFormCollection private class EmptyFormCollection : IFormCollection

View File

@ -36,7 +36,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
{ {
// Silently fail and stop other model binders running if unable to create an instance or use the // Silently fail and stop other model binders running if unable to create an instance or use the
// current instance. // current instance.
bindingContext.Result = ModelBindingResult.Failed(bindingContext.ModelName); bindingContext.Result = ModelBindingResult.Failed();
return; return;
} }
@ -65,7 +65,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
if (postedFiles.Count == 0) if (postedFiles.Count == 0)
{ {
// Silently fail if the named file does not exist in the request. // Silently fail if the named file does not exist in the request.
bindingContext.Result = ModelBindingResult.Failed(bindingContext.ModelName); bindingContext.Result = ModelBindingResult.Failed();
return; return;
} }
@ -77,7 +77,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
{ {
// Silently fail if no files match. Will bind to an empty collection (treat empty as a success // Silently fail if no files match. Will bind to an empty collection (treat empty as a success
// case and not reach here) if binding to a top-level object. // case and not reach here) if binding to a top-level object.
bindingContext.Result = ModelBindingResult.Failed(bindingContext.ModelName); bindingContext.Result = ModelBindingResult.Failed();
return; return;
} }
@ -111,7 +111,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
rawValue: null, rawValue: null,
attemptedValue: null); attemptedValue: null);
bindingContext.Result = ModelBindingResult.Success(bindingContext.ModelName, value); bindingContext.Result = ModelBindingResult.Success(value);
} }
private async Task GetFormFilesAsync( private async Task GetFormFilesAsync(

View File

@ -54,7 +54,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
// Silently fail if unable to create an instance or use the current instance. Also reach here in the // Silently fail if unable to create an instance or use the current instance. Also reach here in the
// typeof(string) case if the header does not exist in the request and in the // typeof(string) case if the header does not exist in the request and in the
// typeof(IEnumerable<string>) case if the header does not exist and this is not a top-level object. // typeof(IEnumerable<string>) case if the header does not exist and this is not a top-level object.
bindingContext.Result = ModelBindingResult.Failed(bindingContext.ModelName); bindingContext.Result = ModelBindingResult.Failed();
} }
else else
{ {
@ -63,7 +63,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
request.Headers.GetCommaSeparatedValues(headerName), request.Headers.GetCommaSeparatedValues(headerName),
request.Headers[headerName]); request.Headers[headerName]);
bindingContext.Result = ModelBindingResult.Success(bindingContext.ModelName, model); bindingContext.Result = ModelBindingResult.Success(model);
} }
return TaskCache.CompletedTask; return TaskCache.CompletedTask;

View File

@ -47,8 +47,11 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
throw new ArgumentNullException(nameof(bindingContext)); throw new ArgumentNullException(nameof(bindingContext));
} }
var keyResult = await TryBindStrongModel<TKey>(bindingContext, _keyBinder, "Key"); var keyModelName = ModelNames.CreatePropertyModelName(bindingContext.ModelName, "Key");
var valueResult = await TryBindStrongModel<TValue>(bindingContext, _valueBinder, "Value"); var keyResult = await TryBindStrongModel<TKey>(bindingContext, _keyBinder, "Key", keyModelName);
var valueModelName = ModelNames.CreatePropertyModelName(bindingContext.ModelName, "Value");
var valueResult = await TryBindStrongModel<TValue>(bindingContext, _valueBinder, "Value", valueModelName);
if (keyResult.IsModelSet && valueResult.IsModelSet) if (keyResult.IsModelSet && valueResult.IsModelSet)
{ {
@ -56,31 +59,31 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
ModelBindingHelper.CastOrDefault<TKey>(keyResult.Model), ModelBindingHelper.CastOrDefault<TKey>(keyResult.Model),
ModelBindingHelper.CastOrDefault<TValue>(valueResult.Model)); ModelBindingHelper.CastOrDefault<TValue>(valueResult.Model));
bindingContext.Result = ModelBindingResult.Success(bindingContext.ModelName, model); bindingContext.Result = ModelBindingResult.Success(model);
return; return;
} }
if (!keyResult.IsModelSet && valueResult.IsModelSet) if (!keyResult.IsModelSet && valueResult.IsModelSet)
{ {
bindingContext.ModelState.TryAddModelError( bindingContext.ModelState.TryAddModelError(
keyResult.Key, keyModelName,
bindingContext.ModelMetadata.ModelBindingMessageProvider.MissingKeyOrValueAccessor()); bindingContext.ModelMetadata.ModelBindingMessageProvider.MissingKeyOrValueAccessor());
// Were able to get some data for this model. // Were able to get some data for this model.
// Always tell the model binding system to skip other model binders. // Always tell the model binding system to skip other model binders.
bindingContext.Result = ModelBindingResult.Failed(bindingContext.ModelName); bindingContext.Result = ModelBindingResult.Failed();
return; return;
} }
if (keyResult.IsModelSet && !valueResult.IsModelSet) if (keyResult.IsModelSet && !valueResult.IsModelSet)
{ {
bindingContext.ModelState.TryAddModelError( bindingContext.ModelState.TryAddModelError(
valueResult.Key, valueModelName,
bindingContext.ModelMetadata.ModelBindingMessageProvider.MissingKeyOrValueAccessor()); bindingContext.ModelMetadata.ModelBindingMessageProvider.MissingKeyOrValueAccessor());
// Were able to get some data for this model. // Were able to get some data for this model.
// Always tell the model binding system to skip other model binders. // Always tell the model binding system to skip other model binders.
bindingContext.Result = ModelBindingResult.Failed(bindingContext.ModelName); bindingContext.Result = ModelBindingResult.Failed();
return; return;
} }
@ -89,17 +92,17 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
if (bindingContext.IsTopLevelObject) if (bindingContext.IsTopLevelObject)
{ {
var model = new KeyValuePair<TKey, TValue>(); var model = new KeyValuePair<TKey, TValue>();
bindingContext.Result = ModelBindingResult.Success(bindingContext.ModelName, model); bindingContext.Result = ModelBindingResult.Success(model);
} }
} }
internal async Task<ModelBindingResult> TryBindStrongModel<TModel>( internal async Task<ModelBindingResult> TryBindStrongModel<TModel>(
ModelBindingContext bindingContext, ModelBindingContext bindingContext,
IModelBinder binder, IModelBinder binder,
string propertyName) string propertyName,
string propertyModelName)
{ {
var propertyModelMetadata = bindingContext.ModelMetadata.Properties[propertyName]; var propertyModelMetadata = bindingContext.ModelMetadata.Properties[propertyName];
var propertyModelName = ModelNames.CreatePropertyModelName(bindingContext.ModelName, propertyName);
using (bindingContext.EnterNestedScope( using (bindingContext.EnterNestedScope(
modelMetadata: propertyModelMetadata, modelMetadata: propertyModelMetadata,
@ -116,7 +119,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
} }
else else
{ {
return ModelBindingResult.Failed(propertyModelName); return ModelBindingResult.Failed();
} }
} }
} }

View File

@ -28,7 +28,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
bindingContext.ValidationState.Add(model, new ValidationStateEntry() { SuppressValidation = true }); bindingContext.ValidationState.Add(model, new ValidationStateEntry() { SuppressValidation = true });
bindingContext.Result = ModelBindingResult.Success(bindingContext.ModelName, model); bindingContext.Result = ModelBindingResult.Success(model);
return TaskCache.CompletedTask; return TaskCache.CompletedTask;
} }
} }

View File

@ -38,7 +38,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
if (valueProviderResult == ValueProviderResult.None) if (valueProviderResult == ValueProviderResult.None)
{ {
// no entry // no entry
bindingContext.Result = ModelBindingResult.Failed(bindingContext.ModelName); bindingContext.Result = ModelBindingResult.Failed();
return TaskCache.CompletedTask; return TaskCache.CompletedTask;
} }
@ -77,12 +77,12 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
bindingContext.ModelMetadata.ModelBindingMessageProvider.ValueMustNotBeNullAccessor( bindingContext.ModelMetadata.ModelBindingMessageProvider.ValueMustNotBeNullAccessor(
valueProviderResult.ToString())); valueProviderResult.ToString()));
bindingContext.Result = ModelBindingResult.Failed(bindingContext.ModelName); bindingContext.Result = ModelBindingResult.Failed();
return TaskCache.CompletedTask; return TaskCache.CompletedTask;
} }
else else
{ {
bindingContext.Result = ModelBindingResult.Success(bindingContext.ModelName, model); bindingContext.Result = ModelBindingResult.Success(model);
return TaskCache.CompletedTask; return TaskCache.CompletedTask;
} }
} }
@ -103,7 +103,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
// Were able to find a converter for the type but conversion failed. // Were able to find a converter for the type but conversion failed.
// Tell the model binding system to skip other model binders. // Tell the model binding system to skip other model binders.
bindingContext.Result = ModelBindingResult.Failed(bindingContext.ModelName); bindingContext.Result = ModelBindingResult.Failed();
return TaskCache.CompletedTask; return TaskCache.CompletedTask;
} }
} }

View File

@ -455,7 +455,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Internal
objectModelValidator.Validate( objectModelValidator.Validate(
actionContext, actionContext,
modelBindingContext.ValidationState, modelBindingContext.ValidationState,
modelBindingResult.Value.Key, modelBindingContext.ModelName,
modelBindingResult.Value.Model); modelBindingResult.Value.Model);
return modelState.IsValid; return modelState.IsValid;

View File

@ -19,7 +19,7 @@ namespace Microsoft.AspNetCore.Mvc.WebApiCompatShim
{ {
var model = bindingContext.HttpContext.GetHttpRequestMessage(); var model = bindingContext.HttpContext.GetHttpRequestMessage();
bindingContext.ValidationState.Add(model, new ValidationStateEntry() { SuppressValidation = true }); bindingContext.ValidationState.Add(model, new ValidationStateEntry() { SuppressValidation = true });
bindingContext.Result = ModelBindingResult.Success(bindingContext.ModelName, model); bindingContext.Result = ModelBindingResult.Success(model);
return TaskCache.CompletedTask; return TaskCache.CompletedTask;
} }

View File

@ -103,7 +103,7 @@ namespace Microsoft.AspNetCore.Mvc.Internal
.Callback((ModelBindingContext context) => .Callback((ModelBindingContext context) =>
{ {
context.ModelMetadata = metadataProvider.GetMetadataForType(typeof(string)); context.ModelMetadata = metadataProvider.GetMetadataForType(typeof(string));
context.Result = ModelBindingResult.Success(string.Empty, value); context.Result = ModelBindingResult.Success(value);
}) })
.Returns(TaskCache.CompletedTask); .Returns(TaskCache.CompletedTask);
var factory = GetModelBinderFactory(binder.Object); var factory = GetModelBinderFactory(binder.Object);
@ -374,7 +374,7 @@ namespace Microsoft.AspNetCore.Mvc.Internal
var controller = new TestController(); var controller = new TestController();
var arguments = new Dictionary<string, object>(StringComparer.Ordinal); var arguments = new Dictionary<string, object>(StringComparer.Ordinal);
var binder = new StubModelBinder(ModelBindingResult.Success(string.Empty, model: null)); var binder = new StubModelBinder(ModelBindingResult.Success(model: null));
var factory = GetModelBinderFactory(binder); var factory = GetModelBinderFactory(binder);
var argumentBinder = GetArgumentBinder(factory); var argumentBinder = GetArgumentBinder(factory);
@ -406,7 +406,7 @@ namespace Microsoft.AspNetCore.Mvc.Internal
var controller = new TestController(); var controller = new TestController();
var arguments = new Dictionary<string, object>(StringComparer.Ordinal); var arguments = new Dictionary<string, object>(StringComparer.Ordinal);
var binder = new StubModelBinder(ModelBindingResult.Success(key: string.Empty, model: null)); var binder = new StubModelBinder(ModelBindingResult.Success(model: null));
var factory = GetModelBinderFactory(binder); var factory = GetModelBinderFactory(binder);
var argumentBinder = GetArgumentBinder(factory); var argumentBinder = GetArgumentBinder(factory);
@ -548,11 +548,11 @@ namespace Microsoft.AspNetCore.Mvc.Internal
object model; object model;
if (inputPropertyValues.TryGetValue(bindingContext.FieldName, out model)) if (inputPropertyValues.TryGetValue(bindingContext.FieldName, out model))
{ {
bindingContext.Result = ModelBindingResult.Success(bindingContext.ModelName, model); bindingContext.Result = ModelBindingResult.Success( model);
} }
else else
{ {
bindingContext.Result = ModelBindingResult.Failed(bindingContext.ModelName); bindingContext.Result = ModelBindingResult.Failed();
} }
}); });
@ -605,7 +605,7 @@ namespace Microsoft.AspNetCore.Mvc.Internal
.Setup(b => b.BindModelAsync(It.IsAny<DefaultModelBindingContext>())) .Setup(b => b.BindModelAsync(It.IsAny<DefaultModelBindingContext>()))
.Returns<DefaultModelBindingContext>(mbc => .Returns<DefaultModelBindingContext>(mbc =>
{ {
mbc.Result = ModelBindingResult.Success(string.Empty, model); mbc.Result = ModelBindingResult.Success(model);
return TaskCache.CompletedTask; return TaskCache.CompletedTask;
}); });

View File

@ -32,7 +32,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
var result = await binder.BindModelResultAsync(bindingContext); var result = await binder.BindModelResultAsync(bindingContext);
// Assert // Assert
Assert.NotEqual(default(ModelBindingResult), result); Assert.True(result.IsModelSet);
var array = Assert.IsType<int[]>(result.Model); var array = Assert.IsType<int[]>(result.Model);
Assert.Equal(new[] { 42, 84 }, array); Assert.Equal(new[] { 42, 84 }, array);
@ -59,10 +59,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
var result = await binder.BindModelResultAsync(context); var result = await binder.BindModelResultAsync(context);
// Assert // Assert
Assert.NotEqual(default(ModelBindingResult), result);
Assert.Empty(Assert.IsType<string[]>(result.Model)); Assert.Empty(Assert.IsType<string[]>(result.Model));
Assert.Equal("modelName", result.Key);
Assert.True(result.IsModelSet); Assert.True(result.IsModelSet);
} }
@ -134,7 +131,6 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
var result = await binder.BindModelResultAsync(bindingContext); var result = await binder.BindModelResultAsync(bindingContext);
// Assert // Assert
Assert.NotEqual(default(ModelBindingResult), result);
Assert.True(result.IsModelSet); Assert.True(result.IsModelSet);
Assert.Same(model, result.Model); Assert.Same(model, result.Model);
@ -153,7 +149,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
if (value != ValueProviderResult.None) if (value != ValueProviderResult.None)
{ {
var model = value.ConvertTo(mbc.ModelType); var model = value.ConvertTo(mbc.ModelType);
return ModelBindingResult.Success(mbc.ModelName, model); return ModelBindingResult.Success(model);
} }
return null; return null;
}); });

View File

@ -4,7 +4,7 @@
using System; using System;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.ModelBinding.Validation; using Microsoft.AspNetCore.Mvc.Internal;
using Microsoft.AspNetCore.Testing; using Microsoft.AspNetCore.Testing;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Moq; using Moq;
@ -26,7 +26,6 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
var binderResult = await binder.BindModelResultAsync(bindingContext); var binderResult = await binder.BindModelResultAsync(bindingContext);
// Assert // Assert
Assert.NotEqual(default(ModelBindingResult), binderResult);
Assert.False(binderResult.IsModelSet); Assert.False(binderResult.IsModelSet);
} }
@ -116,8 +115,8 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
public Task BindModelAsync(ModelBindingContext bindingContext) public Task BindModelAsync(ModelBindingContext bindingContext)
{ {
bindingContext.Result = ModelBindingResult.Success(bindingContext.ModelName, _model); bindingContext.Result = ModelBindingResult.Success(_model);
return Task.FromResult(0); return TaskCache.CompletedTask;
} }
} }
} }

View File

@ -29,7 +29,6 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
// Assert // Assert
Assert.NotNull(binderResult); Assert.NotNull(binderResult);
Assert.False(binderResult.IsModelSet); Assert.False(binderResult.IsModelSet);
Assert.Equal("foo", binderResult.Key);
Assert.Null(binderResult.Model); Assert.Null(binderResult.Model);
var modelState = Assert.Single(bindingContext.ModelState); var modelState = Assert.Single(bindingContext.ModelState);
@ -100,7 +99,6 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
// Assert // Assert
Assert.NotNull(binderResult); Assert.NotNull(binderResult);
Assert.False(binderResult.IsModelSet); Assert.False(binderResult.IsModelSet);
Assert.Equal("foo", binderResult.Key);
Assert.Null(binderResult.Model); Assert.Null(binderResult.Model);
Assert.Empty(bindingContext.ModelState); // No submitted data for "foo". Assert.Empty(bindingContext.ModelState); // No submitted data for "foo".

View File

@ -23,7 +23,6 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
var result = await binder.BindModelResultAsync(bindingContext); var result = await binder.BindModelResultAsync(bindingContext);
// Assert // Assert
Assert.NotEqual(default(ModelBindingResult), result);
Assert.True(result.IsModelSet); Assert.True(result.IsModelSet);
Assert.Equal(bindingContext.HttpContext.RequestAborted, result.Model); Assert.Equal(bindingContext.HttpContext.RequestAborted, result.Model);
} }

View File

@ -84,7 +84,6 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
var result = await binder.BindModelResultAsync(bindingContext); var result = await binder.BindModelResultAsync(bindingContext);
// Assert // Assert
Assert.NotEqual(default(ModelBindingResult), result);
Assert.True(result.IsModelSet); Assert.True(result.IsModelSet);
var list = Assert.IsAssignableFrom<IList<int>>(result.Model); var list = Assert.IsAssignableFrom<IList<int>>(result.Model);
@ -116,7 +115,6 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
var result = await binder.BindModelResultAsync(bindingContext); var result = await binder.BindModelResultAsync(bindingContext);
// Assert // Assert
Assert.NotEqual(default(ModelBindingResult), result);
Assert.True(result.IsModelSet); Assert.True(result.IsModelSet);
Assert.Same(list, result.Model); Assert.Same(list, result.Model);
@ -143,7 +141,6 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
var result = await binder.BindModelResultAsync(bindingContext); var result = await binder.BindModelResultAsync(bindingContext);
// Assert // Assert
Assert.NotEqual(default(ModelBindingResult), result);
Assert.True(result.IsModelSet); Assert.True(result.IsModelSet);
var list = Assert.IsAssignableFrom<IList<int>>(result.Model); var list = Assert.IsAssignableFrom<IList<int>>(result.Model);
@ -170,7 +167,6 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
var result = await binder.BindModelResultAsync(bindingContext); var result = await binder.BindModelResultAsync(bindingContext);
// Assert // Assert
Assert.NotEqual(default(ModelBindingResult), result);
Assert.True(result.IsModelSet); Assert.True(result.IsModelSet);
Assert.Same(list, result.Model); Assert.Same(list, result.Model);
@ -192,7 +188,6 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
var result = await binder.BindModelResultAsync(bindingContext); var result = await binder.BindModelResultAsync(bindingContext);
// Assert // Assert
Assert.NotEqual(default(ModelBindingResult), result);
Assert.True(result.IsModelSet); Assert.True(result.IsModelSet);
Assert.NotNull(result.Model); Assert.NotNull(result.Model);
@ -236,10 +231,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
var result = await binder.BindModelResultAsync(context); var result = await binder.BindModelResultAsync(context);
// Assert // Assert
Assert.NotEqual(default(ModelBindingResult), result);
Assert.Empty(Assert.IsType<List<string>>(result.Model)); Assert.Empty(Assert.IsType<List<string>>(result.Model));
Assert.Equal("modelName", result.Key);
Assert.True(result.IsModelSet); Assert.True(result.IsModelSet);
} }
@ -269,11 +261,8 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
var result = await binder.BindModelResultAsync(context); var result = await binder.BindModelResultAsync(context);
// Assert // Assert
Assert.NotEqual(default(ModelBindingResult), result);
Assert.Same(list, result.Model); Assert.Same(list, result.Model);
Assert.Empty(list); Assert.Empty(list);
Assert.Equal("modelName", result.Key);
Assert.True(result.IsModelSet); Assert.True(result.IsModelSet);
} }
@ -343,7 +332,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
var elementBinder = new StubModelBinder(mbc => var elementBinder = new StubModelBinder(mbc =>
{ {
Assert.Equal("someName", mbc.ModelName); Assert.Equal("someName", mbc.ModelName);
mbc.Result = ModelBindingResult.Success(mbc.ModelName, 42); mbc.Result = ModelBindingResult.Success(42);
}); });
var modelBinder = new CollectionModelBinder<int>(elementBinder); var modelBinder = new CollectionModelBinder<int>(elementBinder);
@ -395,11 +384,11 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
var model = value.ConvertTo(mbc.ModelType); var model = value.ConvertTo(mbc.ModelType);
if (model == null) if (model == null)
{ {
return ModelBindingResult.Failed(mbc.ModelName); return ModelBindingResult.Failed();
} }
else else
{ {
return ModelBindingResult.Success(mbc.ModelName, model); return ModelBindingResult.Success(model);
} }
}); });
} }

View File

@ -540,7 +540,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
var bindingContext = CreateContext(GetMetadataForType(model.GetType()), model); var bindingContext = CreateContext(GetMetadataForType(model.GetType()), model);
var binder = CreateBinder(bindingContext.ModelMetadata); var binder = CreateBinder(bindingContext.ModelMetadata);
binder.Results[property] = ModelBindingResult.Failed("theModel.Age"); binder.Results[property] = ModelBindingResult.Failed();
// Act // Act
await binder.BindModelAsync(bindingContext); await binder.BindModelAsync(bindingContext);
@ -575,7 +575,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
var binder = CreateBinder(bindingContext.ModelMetadata); var binder = CreateBinder(bindingContext.ModelMetadata);
var property = GetMetadataForProperty(model.GetType(), nameof(ModelWithDataMemberIsRequired.Age)); var property = GetMetadataForProperty(model.GetType(), nameof(ModelWithDataMemberIsRequired.Age));
binder.Results[property] = ModelBindingResult.Failed("theModel.Age"); binder.Results[property] = ModelBindingResult.Failed();
// Act // Act
await binder.BindModelAsync(bindingContext); await binder.BindModelAsync(bindingContext);
@ -612,7 +612,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
// Attempt to set non-Nullable property to null. BindRequiredAttribute should not be relevant in this // Attempt to set non-Nullable property to null. BindRequiredAttribute should not be relevant in this
// case because the property did have a result. // case because the property did have a result.
var property = GetMetadataForProperty(model.GetType(), nameof(ModelWithBindRequired.Age)); var property = GetMetadataForProperty(model.GetType(), nameof(ModelWithBindRequired.Age));
binder.Results[property] = ModelBindingResult.Success("theModel.Age", model: null); binder.Results[property] = ModelBindingResult.Success(model: null);
// Act // Act
await binder.BindModelAsync(bindingContext); await binder.BindModelAsync(bindingContext);
@ -642,7 +642,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
var binder = CreateBinder(bindingContext.ModelMetadata); var binder = CreateBinder(bindingContext.ModelMetadata);
var property = GetMetadataForProperty(model.GetType(), nameof(BindingOptionalProperty.ValueTypeRequired)); var property = GetMetadataForProperty(model.GetType(), nameof(BindingOptionalProperty.ValueTypeRequired));
binder.Results[property] = ModelBindingResult.Failed("theModel.ValueTypeRequired"); binder.Results[property] = ModelBindingResult.Failed();
// Act // Act
await binder.BindModelAsync(bindingContext); await binder.BindModelAsync(bindingContext);
@ -662,7 +662,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
var binder = CreateBinder(bindingContext.ModelMetadata); var binder = CreateBinder(bindingContext.ModelMetadata);
var property = GetMetadataForProperty(model.GetType(), nameof(NullableValueTypeProperty.NullableValueType)); var property = GetMetadataForProperty(model.GetType(), nameof(NullableValueTypeProperty.NullableValueType));
binder.Results[property] = ModelBindingResult.Failed("theModel.NullableValueType"); binder.Results[property] = ModelBindingResult.Failed();
// Act // Act
await binder.BindModelAsync(bindingContext); await binder.BindModelAsync(bindingContext);
@ -684,7 +684,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
var binder = CreateBinder(bindingContext.ModelMetadata); var binder = CreateBinder(bindingContext.ModelMetadata);
var property = GetMetadataForProperty(model.GetType(), nameof(Person.ValueTypeRequired)); var property = GetMetadataForProperty(model.GetType(), nameof(Person.ValueTypeRequired));
binder.Results[property] = ModelBindingResult.Failed("theModel." + nameof(Person.ValueTypeRequired)); binder.Results[property] = ModelBindingResult.Failed();
// Act // Act
await binder.BindModelAsync(bindingContext); await binder.BindModelAsync(bindingContext);
@ -706,9 +706,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
var binder = CreateBinder(bindingContext.ModelMetadata); var binder = CreateBinder(bindingContext.ModelMetadata);
var property = GetMetadataForProperty(model.GetType(), nameof(Person.ValueTypeRequired)); var property = GetMetadataForProperty(model.GetType(), nameof(Person.ValueTypeRequired));
binder.Results[property] = ModelBindingResult.Success( binder.Results[property] = ModelBindingResult.Success(model: 57);
key: "theModel." + nameof(Person.ValueTypeRequired),
model: 57);
// Act // Act
await binder.BindModelAsync(bindingContext); await binder.BindModelAsync(bindingContext);
@ -736,18 +734,14 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
foreach (var property in containerMetadata.Properties) foreach (var property in containerMetadata.Properties)
{ {
binder.Results[property] = ModelBindingResult.Failed(property.PropertyName); binder.Results[property] = ModelBindingResult.Failed();
} }
var firstNameProperty = containerMetadata.Properties[nameof(model.FirstName)]; var firstNameProperty = containerMetadata.Properties[nameof(model.FirstName)];
binder.Results[firstNameProperty] = ModelBindingResult.Success( binder.Results[firstNameProperty] = ModelBindingResult.Success("John");
nameof(model.FirstName),
"John");
var lastNameProperty = containerMetadata.Properties[nameof(model.LastName)]; var lastNameProperty = containerMetadata.Properties[nameof(model.LastName)];
binder.Results[lastNameProperty] = ModelBindingResult.Success( binder.Results[lastNameProperty] = ModelBindingResult.Success("Doe");
nameof(model.LastName),
"Doe");
// Act // Act
await binder.BindModelAsync(bindingContext); await binder.BindModelAsync(bindingContext);
@ -769,11 +763,11 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
var metadata = GetMetadataForType(typeof(Person)); var metadata = GetMetadataForType(typeof(Person));
var propertyMetadata = metadata.Properties[nameof(model.PropertyWithDefaultValue)]; var propertyMetadata = metadata.Properties[nameof(model.PropertyWithDefaultValue)];
var result = ModelBindingResult.Failed("foo"); var result = ModelBindingResult.Failed();
var binder = CreateBinder(bindingContext.ModelMetadata); var binder = CreateBinder(bindingContext.ModelMetadata);
// Act // Act
binder.SetPropertyPublic(bindingContext, propertyMetadata, result); binder.SetPropertyPublic(bindingContext, "foo", propertyMetadata, result);
// Assert // Assert
var person = Assert.IsType<Person>(bindingContext.Model); var person = Assert.IsType<Person>(bindingContext.Model);
@ -792,12 +786,12 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
var propertyMetadata = metadata.Properties[nameof(model.PropertyWithInitializedValue)]; var propertyMetadata = metadata.Properties[nameof(model.PropertyWithInitializedValue)];
// The null model value won't be used because IsModelBound = false. // The null model value won't be used because IsModelBound = false.
var result = ModelBindingResult.Failed("foo"); var result = ModelBindingResult.Failed();
var binder = CreateBinder(bindingContext.ModelMetadata); var binder = CreateBinder(bindingContext.ModelMetadata);
// Act // Act
binder.SetPropertyPublic(bindingContext, propertyMetadata, result); binder.SetPropertyPublic(bindingContext, "foo", propertyMetadata, result);
// Assert // Assert
var person = Assert.IsType<Person>(bindingContext.Model); var person = Assert.IsType<Person>(bindingContext.Model);
@ -816,12 +810,12 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
var propertyMetadata = metadata.Properties[nameof(model.PropertyWithInitializedValueAndDefault)]; var propertyMetadata = metadata.Properties[nameof(model.PropertyWithInitializedValueAndDefault)];
// The null model value won't be used because IsModelBound = false. // The null model value won't be used because IsModelBound = false.
var result = ModelBindingResult.Failed("foo"); var result = ModelBindingResult.Failed();
var binder = CreateBinder(bindingContext.ModelMetadata); var binder = CreateBinder(bindingContext.ModelMetadata);
// Act // Act
binder.SetPropertyPublic(bindingContext, propertyMetadata, result); binder.SetPropertyPublic(bindingContext, "foo", propertyMetadata, result);
// Assert // Assert
var person = Assert.IsType<Person>(bindingContext.Model); var person = Assert.IsType<Person>(bindingContext.Model);
@ -839,11 +833,11 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
var metadata = GetMetadataForType(typeof(Person)); var metadata = GetMetadataForType(typeof(Person));
var propertyMetadata = metadata.Properties[nameof(model.NonUpdateableProperty)]; var propertyMetadata = metadata.Properties[nameof(model.NonUpdateableProperty)];
var result = ModelBindingResult.Failed("foo"); var result = ModelBindingResult.Failed();
var binder = CreateBinder(bindingContext.ModelMetadata); var binder = CreateBinder(bindingContext.ModelMetadata);
// Act // Act
binder.SetPropertyPublic(bindingContext, propertyMetadata, result); binder.SetPropertyPublic(bindingContext, "foo", propertyMetadata, result);
// Assert // Assert
// If didn't throw, success! // If didn't throw, success!
@ -882,14 +876,12 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
var metadata = GetMetadataForType(type); var metadata = GetMetadataForType(type);
var propertyMetadata = bindingContext.ModelMetadata.Properties[propertyName]; var propertyMetadata = bindingContext.ModelMetadata.Properties[propertyName];
var result = ModelBindingResult.Success( var result = ModelBindingResult.Success(new Simple { Name = "Hanna" });
propertyName,
new Simple { Name = "Hanna" });
var binder = CreateBinder(bindingContext.ModelMetadata); var binder = CreateBinder(bindingContext.ModelMetadata);
// Act // Act
binder.SetPropertyPublic(bindingContext, propertyMetadata, result); binder.SetPropertyPublic(bindingContext, propertyName, propertyMetadata, result);
// Assert // Assert
Assert.Equal("Joe", propertyAccessor(model)); Assert.Equal("Joe", propertyAccessor(model));
@ -908,12 +900,12 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
var propertyMetadata = GetMetadataForProperty(model.GetType(), nameof(CollectionContainer.ReadOnlyList)); var propertyMetadata = GetMetadataForProperty(model.GetType(), nameof(CollectionContainer.ReadOnlyList));
var bindingContext = CreateContext(modelMetadata, model); var bindingContext = CreateContext(modelMetadata, model);
var result = ModelBindingResult.Success(propertyMetadata.PropertyName, new List<string>() { "hi" }); var result = ModelBindingResult.Success(new List<string>() { "hi" });
var binder = CreateBinder(bindingContext.ModelMetadata); var binder = CreateBinder(bindingContext.ModelMetadata);
// Act // Act
binder.SetPropertyPublic(bindingContext, propertyMetadata, result); binder.SetPropertyPublic(bindingContext, propertyMetadata.PropertyName, propertyMetadata, result);
// Assert // Assert
Assert.Same(originalCollection, model.ReadOnlyList); Assert.Same(originalCollection, model.ReadOnlyList);
@ -930,11 +922,11 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
var metadata = GetMetadataForType(typeof(Person)); var metadata = GetMetadataForType(typeof(Person));
var propertyMetadata = bindingContext.ModelMetadata.Properties[nameof(model.DateOfBirth)]; var propertyMetadata = bindingContext.ModelMetadata.Properties[nameof(model.DateOfBirth)];
var result = ModelBindingResult.Success("foo", new DateTime(2001, 1, 1)); var result = ModelBindingResult.Success(new DateTime(2001, 1, 1));
var binder = CreateBinder(bindingContext.ModelMetadata); var binder = CreateBinder(bindingContext.ModelMetadata);
// Act // Act
binder.SetPropertyPublic(bindingContext, propertyMetadata, result); binder.SetPropertyPublic(bindingContext, "foo", propertyMetadata, result);
// Assert // Assert
Assert.True(bindingContext.ModelState.IsValid); Assert.True(bindingContext.ModelState.IsValid);
@ -956,11 +948,11 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
var metadata = GetMetadataForType(typeof(Person)); var metadata = GetMetadataForType(typeof(Person));
var propertyMetadata = bindingContext.ModelMetadata.Properties[nameof(model.DateOfDeath)]; var propertyMetadata = bindingContext.ModelMetadata.Properties[nameof(model.DateOfDeath)];
var result = ModelBindingResult.Success("foo", new DateTime(1800, 1, 1)); var result = ModelBindingResult.Success(new DateTime(1800, 1, 1));
var binder = CreateBinder(bindingContext.ModelMetadata); var binder = CreateBinder(bindingContext.ModelMetadata);
// Act // Act
binder.SetPropertyPublic(bindingContext, propertyMetadata, result); binder.SetPropertyPublic(bindingContext, "foo", propertyMetadata, result);
// Assert // Assert
Assert.Equal("Date of death can't be before date of birth." + Environment.NewLine Assert.Equal("Date of death can't be before date of birth." + Environment.NewLine
@ -980,11 +972,11 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
var metadata = GetMetadataForType(typeof(ModelWhosePropertySetterThrows)); var metadata = GetMetadataForType(typeof(ModelWhosePropertySetterThrows));
var propertyMetadata = bindingContext.ModelMetadata.Properties[nameof(model.NameNoAttribute)]; var propertyMetadata = bindingContext.ModelMetadata.Properties[nameof(model.NameNoAttribute)];
var result = ModelBindingResult.Success("foo.NameNoAttribute", model: null); var result = ModelBindingResult.Success(model: null);
var binder = CreateBinder(bindingContext.ModelMetadata); var binder = CreateBinder(bindingContext.ModelMetadata);
// Act // Act
binder.SetPropertyPublic(bindingContext, propertyMetadata, result); binder.SetPropertyPublic(bindingContext, "foo.NameNoAttribute", propertyMetadata, result);
// Assert // Assert
Assert.False(bindingContext.ModelState.IsValid); Assert.False(bindingContext.ModelState.IsValid);
@ -1363,18 +1355,20 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
public virtual void SetPropertyPublic( public virtual void SetPropertyPublic(
ModelBindingContext bindingContext, ModelBindingContext bindingContext,
string modelName,
ModelMetadata propertyMetadata, ModelMetadata propertyMetadata,
ModelBindingResult result) ModelBindingResult result)
{ {
base.SetProperty(bindingContext, propertyMetadata, result); base.SetProperty(bindingContext, modelName, propertyMetadata, result);
} }
protected override void SetProperty( protected override void SetProperty(
ModelBindingContext bindingContext, ModelBindingContext bindingContext,
string modelName,
ModelMetadata propertyMetadata, ModelMetadata propertyMetadata,
ModelBindingResult result) ModelBindingResult result)
{ {
SetPropertyPublic(bindingContext, propertyMetadata, result); SetPropertyPublic(bindingContext, modelName, propertyMetadata, result);
} }
} }
} }

View File

@ -150,7 +150,6 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
// Assert // Assert
Assert.True(result.IsModelSet); Assert.True(result.IsModelSet);
Assert.Equal(modelName, result.Key);
var resultDictionary = Assert.IsAssignableFrom<IDictionary<string, string>>(result.Model); var resultDictionary = Assert.IsAssignableFrom<IDictionary<string, string>>(result.Model);
Assert.Equal(dictionary, resultDictionary); Assert.Equal(dictionary, resultDictionary);
@ -186,9 +185,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
var result = await binder.BindModelResultAsync(context); var result = await binder.BindModelResultAsync(context);
// Assert // Assert
Assert.NotEqual(default(ModelBindingResult), result);
Assert.True(result.IsModelSet); Assert.True(result.IsModelSet);
Assert.Equal("prefix", result.Key);
var resultDictionary = Assert.IsAssignableFrom<IDictionary<string, string>>(result.Model); var resultDictionary = Assert.IsAssignableFrom<IDictionary<string, string>>(result.Model);
Assert.Empty(resultDictionary); Assert.Empty(resultDictionary);
@ -238,9 +235,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
var result = await binder.BindModelResultAsync(context); var result = await binder.BindModelResultAsync(context);
// Assert // Assert
Assert.NotEqual(default(ModelBindingResult), result);
Assert.True(result.IsModelSet); Assert.True(result.IsModelSet);
Assert.Equal("prefix", result.Key);
var resultDictionary = Assert.IsAssignableFrom<IDictionary<long, int>>(result.Model); var resultDictionary = Assert.IsAssignableFrom<IDictionary<long, int>>(result.Model);
Assert.Equal(dictionary, resultDictionary); Assert.Equal(dictionary, resultDictionary);
@ -287,9 +282,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
var result = await binder.BindModelResultAsync(context); var result = await binder.BindModelResultAsync(context);
// Assert // Assert
Assert.NotEqual(default(ModelBindingResult), result);
Assert.True(result.IsModelSet); Assert.True(result.IsModelSet);
Assert.Equal("prefix", result.Key);
var resultDictionary = Assert.IsAssignableFrom<IDictionary<int, ModelWithProperties>>(result.Model); var resultDictionary = Assert.IsAssignableFrom<IDictionary<int, ModelWithProperties>>(result.Model);
Assert.Equal(dictionary, resultDictionary); Assert.Equal(dictionary, resultDictionary);
@ -335,9 +328,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
var result = await binder.BindModelResultAsync(context); var result = await binder.BindModelResultAsync(context);
// Assert // Assert
Assert.NotEqual(default(ModelBindingResult), result);
Assert.True(result.IsModelSet); Assert.True(result.IsModelSet);
Assert.Equal(modelName, result.Key);
var resultDictionary = Assert.IsAssignableFrom<SortedDictionary<string, string>>(result.Model); var resultDictionary = Assert.IsAssignableFrom<SortedDictionary<string, string>>(result.Model);
Assert.Equal(expectedDictionary, resultDictionary); Assert.Equal(expectedDictionary, resultDictionary);
@ -366,10 +357,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
var result = await binder.BindModelResultAsync(context); var result = await binder.BindModelResultAsync(context);
// Assert // Assert
Assert.NotEqual(default(ModelBindingResult), result);
Assert.Empty(Assert.IsType<Dictionary<string, string>>(result.Model)); Assert.Empty(Assert.IsType<Dictionary<string, string>>(result.Model));
Assert.Equal("modelName", result.Key);
Assert.True(result.IsModelSet); Assert.True(result.IsModelSet);
} }

View File

@ -32,9 +32,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
var result = await binder.BindModelResultAsync(bindingContext); var result = await binder.BindModelResultAsync(bindingContext);
// Assert // Assert
Assert.NotEqual(default(ModelBindingResult), result);
Assert.True(result.IsModelSet); Assert.True(result.IsModelSet);
Assert.Empty(bindingContext.ValidationState); Assert.Empty(bindingContext.ValidationState);
var form = Assert.IsAssignableFrom<IFormCollection>(result.Model); var form = Assert.IsAssignableFrom<IFormCollection>(result.Model);
@ -55,7 +53,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
var result = await binder.BindModelResultAsync(bindingContext); var result = await binder.BindModelResultAsync(bindingContext);
// Assert // Assert
Assert.NotEqual(default(ModelBindingResult), result); Assert.True(result.IsModelSet);
var form = Assert.IsAssignableFrom<IFormCollection>(result.Model); var form = Assert.IsAssignableFrom<IFormCollection>(result.Model);
Assert.Empty(form); Assert.Empty(form);
} }

View File

@ -29,7 +29,6 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
var result = await binder.BindModelResultAsync(bindingContext); var result = await binder.BindModelResultAsync(bindingContext);
// Assert // Assert
Assert.NotEqual(default(ModelBindingResult), result);
Assert.True(result.IsModelSet); Assert.True(result.IsModelSet);
var entry = bindingContext.ValidationState[result.Model]; var entry = bindingContext.ValidationState[result.Model];
@ -51,7 +50,6 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
var result = await binder.BindModelResultAsync(bindingContext); var result = await binder.BindModelResultAsync(bindingContext);
// Assert // Assert
Assert.NotEqual(default(ModelBindingResult), result);
Assert.True(result.IsModelSet); Assert.True(result.IsModelSet);
var entry = bindingContext.ValidationState[result.Model]; var entry = bindingContext.ValidationState[result.Model];
@ -84,7 +82,6 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
var result = await binder.BindModelResultAsync(bindingContext); var result = await binder.BindModelResultAsync(bindingContext);
// Assert // Assert
Assert.NotEqual(default(ModelBindingResult), result);
Assert.True(result.IsModelSet); Assert.True(result.IsModelSet);
Assert.IsAssignableFrom(destinationType, result.Model); Assert.IsAssignableFrom(destinationType, result.Model);
Assert.Equal(formFiles, result.Model as IEnumerable<IFormFile>); Assert.Equal(formFiles, result.Model as IEnumerable<IFormFile>);
@ -103,7 +100,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
var result = await binder.BindModelResultAsync(bindingContext); var result = await binder.BindModelResultAsync(bindingContext);
// Assert // Assert
Assert.NotEqual(default(ModelBindingResult), result); Assert.True(result.IsModelSet);
var file = Assert.IsAssignableFrom<IFormFile>(result.Model); var file = Assert.IsAssignableFrom<IFormFile>(result.Model);
Assert.Equal("file1.txt", file.FileName); Assert.Equal("file1.txt", file.FileName);
} }
@ -121,7 +118,6 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
var result = await binder.BindModelResultAsync(bindingContext); var result = await binder.BindModelResultAsync(bindingContext);
// Assert // Assert
Assert.NotEqual(default(ModelBindingResult), result);
Assert.False(result.IsModelSet); Assert.False(result.IsModelSet);
Assert.Null(result.Model); Assert.Null(result.Model);
} }
@ -140,7 +136,6 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
var result = await binder.BindModelResultAsync(bindingContext); var result = await binder.BindModelResultAsync(bindingContext);
// Assert // Assert
Assert.NotEqual(default(ModelBindingResult), result);
Assert.False(result.IsModelSet); Assert.False(result.IsModelSet);
Assert.Null(result.Model); Assert.Null(result.Model);
} }
@ -167,7 +162,6 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
var result = await binder.BindModelResultAsync(bindingContext); var result = await binder.BindModelResultAsync(bindingContext);
// Assert // Assert
Assert.NotEqual(default(ModelBindingResult), result);
Assert.True(result.IsModelSet); Assert.True(result.IsModelSet);
var file = Assert.IsAssignableFrom<IFormFile>(result.Model); var file = Assert.IsAssignableFrom<IFormFile>(result.Model);
@ -188,7 +182,6 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
var result = await binder.BindModelResultAsync(bindingContext); var result = await binder.BindModelResultAsync(bindingContext);
// Assert // Assert
Assert.NotEqual(default(ModelBindingResult), result);
Assert.False(result.IsModelSet); Assert.False(result.IsModelSet);
Assert.Null(result.Model); Assert.Null(result.Model);
} }
@ -207,7 +200,6 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
var result = await binder.BindModelResultAsync(bindingContext); var result = await binder.BindModelResultAsync(bindingContext);
// Assert // Assert
Assert.NotEqual(default(ModelBindingResult), result);
Assert.False(result.IsModelSet); Assert.False(result.IsModelSet);
Assert.Null(result.Model); Assert.Null(result.Model);
} }
@ -225,7 +217,6 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
var result = await binder.BindModelResultAsync(bindingContext); var result = await binder.BindModelResultAsync(bindingContext);
// Assert // Assert
Assert.NotEqual(default(ModelBindingResult), result);
Assert.False(result.IsModelSet); Assert.False(result.IsModelSet);
Assert.Null(result.Model); Assert.Null(result.Model);
} }
@ -243,7 +234,6 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
var result = await binder.BindModelResultAsync(bindingContext); var result = await binder.BindModelResultAsync(bindingContext);
// Assert // Assert
Assert.NotEqual(default(ModelBindingResult), result);
Assert.False(result.IsModelSet); Assert.False(result.IsModelSet);
Assert.Null(result.Model); Assert.Null(result.Model);
} }

View File

@ -28,7 +28,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
var result = await binder.BindModelResultAsync(modelBindingContext); var result = await binder.BindModelResultAsync(modelBindingContext);
// Assert // Assert
Assert.NotEqual(default(ModelBindingResult), result); Assert.False(result.IsModelSet);
} }
[Fact] [Fact]
@ -48,7 +48,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
var result = await binder.BindModelResultAsync(modelBindingContext); var result = await binder.BindModelResultAsync(modelBindingContext);
// Assert // Assert
Assert.NotEqual(default(ModelBindingResult), result); Assert.True(result.IsModelSet);
Assert.Equal(headerValue.Split(','), result.Model); Assert.Equal(headerValue.Split(','), result.Model);
} }
@ -69,7 +69,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
var result = await binder.BindModelResultAsync(modelBindingContext); var result = await binder.BindModelResultAsync(modelBindingContext);
// Assert // Assert
Assert.NotEqual(default(ModelBindingResult), result); Assert.True(result.IsModelSet);
Assert.Equal(headerValue, result.Model); Assert.Equal(headerValue, result.Model);
} }
@ -95,7 +95,6 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
var result = await binder.BindModelResultAsync(modelBindingContext); var result = await binder.BindModelResultAsync(modelBindingContext);
// Assert // Assert
Assert.NotEqual(default(ModelBindingResult), result);
Assert.True(result.IsModelSet); Assert.True(result.IsModelSet);
Assert.IsAssignableFrom(destinationType, result.Model); Assert.IsAssignableFrom(destinationType, result.Model);
Assert.Equal(headerValue.Split(','), result.Model as IEnumerable<string>); Assert.Equal(headerValue.Split(','), result.Model as IEnumerable<string>);
@ -117,9 +116,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
var result = await binder.BindModelResultAsync(modelBindingContext); var result = await binder.BindModelResultAsync(modelBindingContext);
// Assert // Assert
Assert.NotEqual(default(ModelBindingResult), result);
Assert.False(result.IsModelSet); Assert.False(result.IsModelSet);
Assert.Equal("modelName", result.Key);
Assert.Null(result.Model); Assert.Null(result.Model);
} }
@ -139,9 +136,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
var result = await binder.BindModelResultAsync(modelBindingContext); var result = await binder.BindModelResultAsync(modelBindingContext);
// Assert // Assert
Assert.NotEqual(default(ModelBindingResult), result);
Assert.False(result.IsModelSet); Assert.False(result.IsModelSet);
Assert.Equal("modelName", result.Key);
Assert.Null(result.Model); Assert.Null(result.Model);
} }

View File

@ -27,7 +27,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
var result = await binder.BindModelResultAsync(bindingContext); var result = await binder.BindModelResultAsync(bindingContext);
// Assert // Assert
Assert.NotEqual(default(ModelBindingResult), result); Assert.False(result.IsModelSet);
Assert.Null(result.Model); Assert.Null(result.Model);
Assert.False(bindingContext.ModelState.IsValid); Assert.False(bindingContext.ModelState.IsValid);
Assert.Equal("someName", bindingContext.ModelName); Assert.Equal("someName", bindingContext.ModelName);
@ -90,7 +90,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
var result = await binder.BindModelResultAsync(bindingContext); var result = await binder.BindModelResultAsync(bindingContext);
// Assert // Assert
Assert.NotEqual(default(ModelBindingResult), result); Assert.True(result.IsModelSet);
Assert.Equal(new KeyValuePair<int, string>(42, "some-value"), result.Model); Assert.Equal(new KeyValuePair<int, string>(42, "some-value"), result.Model);
} }
@ -106,11 +106,11 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
ModelBindingResult? innerResult; ModelBindingResult? innerResult;
if (isSuccess) if (isSuccess)
{ {
innerResult = ModelBindingResult.Success("somename.Key", model); innerResult = ModelBindingResult.Success(model);
} }
else else
{ {
innerResult = ModelBindingResult.Failed("somename.Key"); innerResult = ModelBindingResult.Failed();
} }
var innerBinder = new StubModelBinder(context => var innerBinder = new StubModelBinder(context =>
@ -125,7 +125,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
var binder = new KeyValuePairModelBinder<int, string>(innerBinder, innerBinder); var binder = new KeyValuePairModelBinder<int, string>(innerBinder, innerBinder);
// Act // Act
var result = await binder.TryBindStrongModel<int>(bindingContext, innerBinder, "Key"); var result = await binder.TryBindStrongModel<int>(bindingContext, innerBinder, "Key", "someName.Key");
// Assert // Assert
Assert.Equal(innerResult.Value, result); Assert.Equal(innerResult.Value, result);
@ -155,11 +155,8 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
var result = await binder.BindModelResultAsync(context); var result = await binder.BindModelResultAsync(context);
// Assert // Assert
Assert.NotEqual(default(ModelBindingResult), result);
var model = Assert.IsType<KeyValuePair<string, string>>(result.Model); var model = Assert.IsType<KeyValuePair<string, string>>(result.Model);
Assert.Equal(default(KeyValuePair<string, string>), model); Assert.Equal(default(KeyValuePair<string, string>), model);
Assert.Equal("modelName", result.Key);
Assert.True(result.IsModelSet); Assert.True(result.IsModelSet);
} }
@ -226,7 +223,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
if (mbc.ModelType == typeof(int) && success) if (mbc.ModelType == typeof(int) && success)
{ {
var model = 42; var model = 42;
return ModelBindingResult.Success(mbc.ModelName, model); return ModelBindingResult.Success(model);
} }
return null; return null;
}); });
@ -240,7 +237,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
if (mbc.ModelType == typeof(string) && success) if (mbc.ModelType == typeof(string) && success)
{ {
var model = "some-value"; var model = "some-value";
return ModelBindingResult.Success(mbc.ModelName, model); return ModelBindingResult.Success(model);
} }
return null; return null;
}); });

View File

@ -25,10 +25,8 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
var result = await binder.BindModelResultAsync(modelBindingContext); var result = await binder.BindModelResultAsync(modelBindingContext);
// Assert // Assert
Assert.NotEqual(default(ModelBindingResult), result);
Assert.True(result.IsModelSet); Assert.True(result.IsModelSet);
Assert.NotNull(result.Model); Assert.NotNull(result.Model);
Assert.Equal("modelName", result.Key);
var entry = modelBindingContext.ValidationState[result.Model]; var entry = modelBindingContext.ValidationState[result.Model];
Assert.True(entry.SuppressValidation); Assert.True(entry.SuppressValidation);

View File

@ -53,7 +53,6 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
var result = await binder.BindModelResultAsync(bindingContext); var result = await binder.BindModelResultAsync(bindingContext);
// Assert // Assert
Assert.NotEqual(default(ModelBindingResult), result);
Assert.False(result.IsModelSet); Assert.False(result.IsModelSet);
} }
@ -104,7 +103,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
var result = await binder.BindModelResultAsync(bindingContext); var result = await binder.BindModelResultAsync(bindingContext);
// Assert // Assert
Assert.NotEqual(default(ModelBindingResult), result); Assert.False(result.IsModelSet);
Assert.Null(result.Model); Assert.Null(result.Model);
Assert.False(bindingContext.ModelState.IsValid); Assert.False(bindingContext.ModelState.IsValid);
var error = Assert.Single(bindingContext.ModelState["theModelName"].Errors); var error = Assert.Single(bindingContext.ModelState["theModelName"].Errors);
@ -122,7 +121,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders
var result = await binder.BindModelResultAsync(bindingContext); var result = await binder.BindModelResultAsync(bindingContext);
// Assert // Assert
Assert.Equal(ModelBindingResult.Failed("theModelName"), result); Assert.Equal(ModelBindingResult.Failed(), result);
Assert.Empty(bindingContext.ModelState); Assert.Empty(bindingContext.ModelState);
} }

View File

@ -11,11 +11,9 @@ using System.Threading.Tasks;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.DataAnnotations; using Microsoft.AspNetCore.Mvc.DataAnnotations;
using Microsoft.AspNetCore.Mvc.DataAnnotations.Internal; using Microsoft.AspNetCore.Mvc.DataAnnotations.Internal;
using Microsoft.AspNetCore.Mvc.Formatters;
using Microsoft.AspNetCore.Mvc.Internal; using Microsoft.AspNetCore.Mvc.Internal;
using Microsoft.AspNetCore.Mvc.ModelBinding.Binders; using Microsoft.AspNetCore.Mvc.ModelBinding.Binders;
using Microsoft.AspNetCore.Mvc.ModelBinding.Internal; using Microsoft.AspNetCore.Mvc.ModelBinding.Internal;
using Microsoft.AspNetCore.Mvc.ModelBinding.Test;
using Microsoft.AspNetCore.Mvc.ModelBinding.Validation; using Microsoft.AspNetCore.Mvc.ModelBinding.Validation;
using Moq; using Moq;
using Xunit; using Xunit;
@ -31,7 +29,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding
return new TheoryData<ModelBindingResult?> return new TheoryData<ModelBindingResult?>
{ {
null, null,
ModelBindingResult.Failed("someKey"), ModelBindingResult.Failed(),
}; };
} }
} }

View File

@ -1,7 +1,6 @@
// Copyright (c) .NET Foundation. All rights reserved. // Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Threading.Tasks;
using Xunit; using Xunit;
namespace Microsoft.AspNetCore.Mvc.ModelBinding.Test namespace Microsoft.AspNetCore.Mvc.ModelBinding.Test
@ -12,14 +11,12 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Test
public void Success_SetsProperties() public void Success_SetsProperties()
{ {
// Arrange // Arrange
var key = "someName";
var model = "some model"; var model = "some model";
// Act // Act
var result = ModelBindingResult.Success(key, model); var result = ModelBindingResult.Success(model);
// Assert // Assert
Assert.Same(key, result.Key);
Assert.True(result.IsModelSet); Assert.True(result.IsModelSet);
Assert.Same(model, result.Model); Assert.Same(model, result.Model);
} }
@ -27,14 +24,10 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Test
[Fact] [Fact]
public void Failed_SetsProperties() public void Failed_SetsProperties()
{ {
// Arrange // Arrange & Act
var key = "someName"; var result = ModelBindingResult.Failed();
// Act
var result = ModelBindingResult.Failed(key);
// Assert // Assert
Assert.Same(key, result.Key);
Assert.False(result.IsModelSet); Assert.False(result.IsModelSet);
Assert.Null(result.Model); Assert.Null(result.Model);
} }

View File

@ -181,8 +181,8 @@ namespace Microsoft.AspNetCore.Mvc.IntegrationTests
// Assert // Assert
// ModelBindingResult // ModelBindingResult
Assert.True(modelBindingResult.IsModelSet);
Assert.Equal("Success", modelBindingResult.Model); Assert.Equal("Success", modelBindingResult.Model);
Assert.Equal("CustomParameter", modelBindingResult.Key);
// ModelState // ModelState
Assert.True(modelState.IsValid); Assert.True(modelState.IsValid);
@ -225,7 +225,6 @@ namespace Microsoft.AspNetCore.Mvc.IntegrationTests
// ModelBindingResult // ModelBindingResult
Assert.True(modelBindingResult.IsModelSet); Assert.True(modelBindingResult.IsModelSet);
Assert.Equal(string.Empty, modelBindingResult.Key);
// Model // Model
var boundPerson = Assert.IsType<Person>(modelBindingResult.Model); var boundPerson = Assert.IsType<Person>(modelBindingResult.Model);
@ -264,7 +263,7 @@ namespace Microsoft.AspNetCore.Mvc.IntegrationTests
// Assert // Assert
// ModelBindingResult // ModelBindingResult
Assert.Equal("CustomParameter", modelBindingResult.Key); Assert.True(modelBindingResult.IsModelSet);
// Model // Model
var boundPerson = Assert.IsType<Person>(modelBindingResult.Model); var boundPerson = Assert.IsType<Person>(modelBindingResult.Model);
@ -301,7 +300,7 @@ namespace Microsoft.AspNetCore.Mvc.IntegrationTests
new string[] { address.Street }, new string[] { address.Street },
address.Street); address.Street);
bindingContext.Result = ModelBindingResult.Success(bindingContext.ModelName, address); bindingContext.Result = ModelBindingResult.Success(address);
return TaskCache.CompletedTask; return TaskCache.CompletedTask;
} }
} }
@ -322,7 +321,7 @@ namespace Microsoft.AspNetCore.Mvc.IntegrationTests
new string[] { model }, new string[] { model },
model); model);
bindingContext.Result =ModelBindingResult.Success(bindingContext.ModelName, model); bindingContext.Result =ModelBindingResult.Success(model);
return TaskCache.CompletedTask; return TaskCache.CompletedTask;
} }
} }
@ -337,7 +336,7 @@ namespace Microsoft.AspNetCore.Mvc.IntegrationTests
} }
Debug.Assert(bindingContext.Result == null); Debug.Assert(bindingContext.Result == null);
bindingContext.Result = ModelBindingResult.Success(bindingContext.ModelName, model: null); bindingContext.Result = ModelBindingResult.Success(model: null);
return TaskCache.CompletedTask; return TaskCache.CompletedTask;
} }
} }
@ -352,7 +351,7 @@ namespace Microsoft.AspNetCore.Mvc.IntegrationTests
} }
Debug.Assert(bindingContext.Result == null); Debug.Assert(bindingContext.Result == null);
bindingContext.Result = ModelBindingResult.Failed(bindingContext.ModelName); bindingContext.Result = ModelBindingResult.Failed();
return TaskCache.CompletedTask; return TaskCache.CompletedTask;
} }
} }

View File

@ -186,7 +186,7 @@ namespace Microsoft.AspNetCore.Mvc.IntegrationTests
return TaskCache.CompletedTask; return TaskCache.CompletedTask;
} }
bindingContext.Result = ModelBindingResult.Success(bindingContext.ModelName, new Address()); bindingContext.Result = ModelBindingResult.Success(new Address());
return TaskCache.CompletedTask; return TaskCache.CompletedTask;
} }
} }

View File

@ -51,7 +51,6 @@ namespace Microsoft.AspNetCore.Mvc.IntegrationTests
// Assert // Assert
Assert.True(modelBindingResult.IsModelSet); Assert.True(modelBindingResult.IsModelSet);
Assert.Equal("parameter", modelBindingResult.Key);
var model = Assert.IsType<Person1>(modelBindingResult.Model); var model = Assert.IsType<Person1>(modelBindingResult.Model);
Assert.Null(model.Name); Assert.Null(model.Name);
@ -101,7 +100,6 @@ namespace Microsoft.AspNetCore.Mvc.IntegrationTests
// Assert // Assert
Assert.True(modelBindingResult.IsModelSet); Assert.True(modelBindingResult.IsModelSet);
Assert.Equal("parameter", modelBindingResult.Key);
var model = Assert.IsType<Person2>(modelBindingResult.Model); var model = Assert.IsType<Person2>(modelBindingResult.Model);
Assert.Null(model.Name); Assert.Null(model.Name);
@ -141,7 +139,6 @@ namespace Microsoft.AspNetCore.Mvc.IntegrationTests
// Assert // Assert
Assert.True(modelBindingResult.IsModelSet); Assert.True(modelBindingResult.IsModelSet);
Assert.Equal(string.Empty, modelBindingResult.Key);
var model = Assert.IsType<Person3>(modelBindingResult.Model); var model = Assert.IsType<Person3>(modelBindingResult.Model);
Assert.Null(model.Name); Assert.Null(model.Name);
@ -191,7 +188,6 @@ namespace Microsoft.AspNetCore.Mvc.IntegrationTests
// Assert // Assert
Assert.True(modelBindingResult.IsModelSet); Assert.True(modelBindingResult.IsModelSet);
Assert.Equal(string.Empty, modelBindingResult.Key);
var model = Assert.IsType<Person4>(modelBindingResult.Model); var model = Assert.IsType<Person4>(modelBindingResult.Model);
Assert.Null(model.Name); Assert.Null(model.Name);
@ -238,7 +234,6 @@ namespace Microsoft.AspNetCore.Mvc.IntegrationTests
// Assert // Assert
Assert.True(modelBindingResult.IsModelSet); Assert.True(modelBindingResult.IsModelSet);
Assert.Equal(string.Empty, modelBindingResult.Key);
var model = Assert.IsType<Person5>(modelBindingResult.Model); var model = Assert.IsType<Person5>(modelBindingResult.Model);
Assert.Null(model.Name); Assert.Null(model.Name);

View File

@ -462,7 +462,7 @@ namespace Microsoft.AspNetCore.Mvc.IntegrationTests
// Assert // Assert
// ModelBindingResult // ModelBindingResult
Assert.Equal(ModelBindingResult.Failed(string.Empty), modelBindingResult); Assert.Equal(ModelBindingResult.Failed(), modelBindingResult);
// ModelState // ModelState
Assert.True(modelState.IsValid); Assert.True(modelState.IsValid);