diff --git a/src/Microsoft.AspNet.Mvc.Abstractions/ModelBinding/ModelBindingContext.cs b/src/Microsoft.AspNet.Mvc.Abstractions/ModelBinding/ModelBindingContext.cs
index e32b97f063..a89d58fe6c 100644
--- a/src/Microsoft.AspNet.Mvc.Abstractions/ModelBinding/ModelBindingContext.cs
+++ b/src/Microsoft.AspNet.Mvc.Abstractions/ModelBinding/ModelBindingContext.cs
@@ -37,7 +37,6 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
/// A new instance of .
public static ModelBindingContext CreateBindingContext(
OperationBindingContext operationBindingContext,
- ModelStateDictionary modelState,
ModelMetadata metadata,
BindingInfo bindingInfo,
string modelName)
@@ -47,11 +46,6 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
throw new ArgumentNullException(nameof(operationBindingContext));
}
- if (modelState == null)
- {
- throw new ArgumentNullException(nameof(modelState));
- }
-
if (metadata == null)
{
throw new ArgumentNullException(nameof(metadata));
@@ -83,7 +77,7 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
IsTopLevelObject = true,
ModelMetadata = metadata,
- ModelState = modelState,
+ ModelState = operationBindingContext.ActionContext.ModelState,
OperationBindingContext = operationBindingContext,
ValueProvider = operationBindingContext.ValueProvider,
diff --git a/src/Microsoft.AspNet.Mvc.Abstractions/ModelBinding/Validation/ClientModelValidationContext.cs b/src/Microsoft.AspNet.Mvc.Abstractions/ModelBinding/Validation/ClientModelValidationContext.cs
index e1a8d30e00..037f28bbe7 100644
--- a/src/Microsoft.AspNet.Mvc.Abstractions/ModelBinding/Validation/ClientModelValidationContext.cs
+++ b/src/Microsoft.AspNet.Mvc.Abstractions/ModelBinding/Validation/ClientModelValidationContext.cs
@@ -8,10 +8,15 @@ namespace Microsoft.AspNet.Mvc.ModelBinding.Validation
public class ClientModelValidationContext
{
public ClientModelValidationContext(
+ ActionContext actionContext,
ModelMetadata metadata,
- IModelMetadataProvider metadataProvider,
- IServiceProvider requestServices)
+ IModelMetadataProvider metadataProvider)
{
+ if (actionContext == null)
+ {
+ throw new ArgumentNullException(nameof(actionContext));
+ }
+
if (metadata == null)
{
throw new ArgumentNullException(nameof(metadata));
@@ -22,20 +27,15 @@ namespace Microsoft.AspNet.Mvc.ModelBinding.Validation
throw new ArgumentNullException(nameof(metadataProvider));
}
- if (requestServices == null)
- {
- throw new ArgumentNullException(nameof(requestServices));
- }
-
+ ActionContext = actionContext;
ModelMetadata = metadata;
MetadataProvider = metadataProvider;
- RequestServices = requestServices;
}
+ public ActionContext ActionContext { get; }
+
public ModelMetadata ModelMetadata { get; }
public IModelMetadataProvider MetadataProvider { get; }
-
- public IServiceProvider RequestServices { get; }
}
}
diff --git a/src/Microsoft.AspNet.Mvc.Abstractions/ModelBinding/Validation/ModelValidationContext.cs b/src/Microsoft.AspNet.Mvc.Abstractions/ModelBinding/Validation/ModelValidationContext.cs
index d600ce7e94..cf8c72b249 100644
--- a/src/Microsoft.AspNet.Mvc.Abstractions/ModelBinding/Validation/ModelValidationContext.cs
+++ b/src/Microsoft.AspNet.Mvc.Abstractions/ModelBinding/Validation/ModelValidationContext.cs
@@ -8,6 +8,11 @@ namespace Microsoft.AspNet.Mvc.ModelBinding.Validation
///
public class ModelValidationContext
{
+ ///
+ /// Gets or sets the
+ ///
+ public ActionContext ActionContext { get; set; }
+
///
/// Gets or sets the model object.
///
diff --git a/src/Microsoft.AspNet.Mvc.Core/Controllers/DefaultControllerActionArgumentBinder.cs b/src/Microsoft.AspNet.Mvc.Core/Controllers/DefaultControllerActionArgumentBinder.cs
index 2d90a3caa9..34d8d2acdd 100644
--- a/src/Microsoft.AspNet.Mvc.Core/Controllers/DefaultControllerActionArgumentBinder.cs
+++ b/src/Microsoft.AspNet.Mvc.Core/Controllers/DefaultControllerActionArgumentBinder.cs
@@ -82,7 +82,6 @@ namespace Microsoft.AspNet.Mvc.Controllers
var controllerProperties = new Dictionary(StringComparer.Ordinal);
await PopulateArgumentsAsync(
operationBindingContext,
- context.ModelState,
controllerProperties,
actionDescriptor.BoundProperties);
var controllerType = actionDescriptor.ControllerTypeInfo.AsType();
@@ -91,7 +90,6 @@ namespace Microsoft.AspNet.Mvc.Controllers
var actionArguments = new Dictionary(StringComparer.Ordinal);
await PopulateArgumentsAsync(
operationBindingContext,
- context.ModelState,
actionArguments,
actionDescriptor.Parameters);
return actionArguments;
@@ -99,7 +97,6 @@ namespace Microsoft.AspNet.Mvc.Controllers
public async Task BindModelAsync(
ParameterDescriptor parameter,
- ModelStateDictionary modelState,
OperationBindingContext operationContext)
{
if (parameter == null)
@@ -107,11 +104,6 @@ namespace Microsoft.AspNet.Mvc.Controllers
throw new ArgumentNullException(nameof(parameter));
}
- if (modelState == null)
- {
- throw new ArgumentNullException(nameof(modelState));
- }
-
if (operationContext == null)
{
throw new ArgumentNullException(nameof(operationContext));
@@ -120,7 +112,6 @@ namespace Microsoft.AspNet.Mvc.Controllers
var metadata = _modelMetadataProvider.GetMetadataForType(parameter.ParameterType);
var modelBindingContext = ModelBindingContext.CreateBindingContext(
operationContext,
- modelState,
metadata,
parameter.BindingInfo,
parameter.Name);
@@ -129,8 +120,8 @@ namespace Microsoft.AspNet.Mvc.Controllers
if (modelBindingResult.IsModelSet)
{
_validator.Validate(
+ operationContext.ActionContext,
operationContext.ValidatorProvider,
- modelState,
modelBindingContext.ValidationState,
modelBindingResult.Key,
modelBindingResult.Model);
@@ -204,7 +195,6 @@ namespace Microsoft.AspNet.Mvc.Controllers
private async Task PopulateArgumentsAsync(
OperationBindingContext operationContext,
- ModelStateDictionary modelState,
IDictionary arguments,
IList parameterMetadata)
{
@@ -212,7 +202,7 @@ namespace Microsoft.AspNet.Mvc.Controllers
for (var i = 0; i < parameterMetadata.Count; i++)
{
var parameter = parameterMetadata[i];
- var modelBindingResult = await BindModelAsync(parameter, modelState, operationContext);
+ var modelBindingResult = await BindModelAsync(parameter, operationContext);
if (modelBindingResult.IsModelSet)
{
arguments[parameter.Name] = modelBindingResult.Model;
diff --git a/src/Microsoft.AspNet.Mvc.Core/ModelBinding/ModelBindingHelper.cs b/src/Microsoft.AspNet.Mvc.Core/ModelBinding/ModelBindingHelper.cs
index b42b31c55e..8764339924 100644
--- a/src/Microsoft.AspNet.Mvc.Core/ModelBinding/ModelBindingHelper.cs
+++ b/src/Microsoft.AspNet.Mvc.Core/ModelBinding/ModelBindingHelper.cs
@@ -29,8 +29,6 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
/// The prefix to use when looking up values in the .
///
/// The for the current executing request.
- /// The used for maintaining state and
- /// results of model-binding validation.
/// The provider used for reading metadata for the model type.
/// The used for binding.
/// The used for looking up values.
@@ -46,7 +44,6 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
TModel model,
string prefix,
ActionContext actionContext,
- ModelStateDictionary modelState,
IModelMetadataProvider metadataProvider,
IModelBinder modelBinder,
IValueProvider valueProvider,
@@ -70,11 +67,6 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
throw new ArgumentNullException(nameof(actionContext));
}
- if (modelState == null)
- {
- throw new ArgumentNullException(nameof(modelState));
- }
-
if (metadataProvider == null)
{
throw new ArgumentNullException(nameof(metadataProvider));
@@ -110,7 +102,6 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
model,
prefix,
actionContext,
- modelState,
metadataProvider,
modelBinder,
valueProvider,
@@ -130,8 +121,6 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
/// The prefix to use when looking up values in the .
///
/// The for the current executing request.
- /// The used for maintaining state and
- /// results of model-binding validation.
/// The provider used for reading metadata for the model type.
/// The used for binding.
/// The used for looking up values.
@@ -147,17 +136,16 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
/// which need to be included for the current model.
/// A that on completion returns true if the update is successful
public static Task TryUpdateModelAsync(
- TModel model,
- string prefix,
- ActionContext actionContext,
- ModelStateDictionary modelState,
- IModelMetadataProvider metadataProvider,
- IModelBinder modelBinder,
- IValueProvider valueProvider,
- IList inputFormatters,
- IObjectModelValidator objectModelValidator,
- IModelValidatorProvider validatorProvider,
- params Expression>[] includeExpressions)
+ TModel model,
+ string prefix,
+ ActionContext actionContext,
+ IModelMetadataProvider metadataProvider,
+ IModelBinder modelBinder,
+ IValueProvider valueProvider,
+ IList inputFormatters,
+ IObjectModelValidator objectModelValidator,
+ IModelValidatorProvider validatorProvider,
+ params Expression>[] includeExpressions)
where TModel : class
{
if (model == null)
@@ -175,11 +163,6 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
throw new ArgumentNullException(nameof(actionContext));
}
- if (modelState == null)
- {
- throw new ArgumentNullException(nameof(modelState));
- }
-
if (metadataProvider == null)
{
throw new ArgumentNullException(nameof(metadataProvider));
@@ -222,7 +205,6 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
model,
prefix,
actionContext,
- modelState,
metadataProvider,
modelBinder,
valueProvider,
@@ -242,8 +224,6 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
/// The prefix to use when looking up values in the .
///
/// The for the current executing request.
- /// The used for maintaining state and
- /// results of model-binding validation.
/// The provider used for reading metadata for the model type.
/// The used for binding.
/// The used for looking up values.
@@ -258,18 +238,17 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
/// filter properties(for inclusion/exclusion) at runtime.
/// A that on completion returns true if the update is successful
public static Task TryUpdateModelAsync(
- TModel model,
- string prefix,
- ActionContext actionContext,
- ModelStateDictionary modelState,
- IModelMetadataProvider metadataProvider,
- IModelBinder modelBinder,
- IValueProvider valueProvider,
- IList inputFormatters,
- IObjectModelValidator objectModelValidator,
- IModelValidatorProvider validatorProvider,
- Func predicate)
- where TModel : class
+ TModel model,
+ string prefix,
+ ActionContext actionContext,
+ IModelMetadataProvider metadataProvider,
+ IModelBinder modelBinder,
+ IValueProvider valueProvider,
+ IList inputFormatters,
+ IObjectModelValidator objectModelValidator,
+ IModelValidatorProvider validatorProvider,
+ Func predicate)
+ where TModel : class
{
if (model == null)
{
@@ -286,11 +265,6 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
throw new ArgumentNullException(nameof(actionContext));
}
- if (modelState == null)
- {
- throw new ArgumentNullException(nameof(modelState));
- }
-
if (metadataProvider == null)
{
throw new ArgumentNullException(nameof(metadataProvider));
@@ -331,7 +305,6 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
typeof(TModel),
prefix,
actionContext,
- modelState,
metadataProvider,
modelBinder,
valueProvider,
@@ -351,8 +324,6 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
/// The prefix to use when looking up values in the .
///
/// The for the current executing request.
- /// The used for maintaining state and
- /// results of model-binding validation.
/// The provider used for reading metadata for the model type.
/// The used for binding.
/// The used for looking up values.
@@ -369,7 +340,6 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
Type modelType,
string prefix,
ActionContext actionContext,
- ModelStateDictionary modelState,
IModelMetadataProvider metadataProvider,
IModelBinder modelBinder,
IValueProvider valueProvider,
@@ -397,11 +367,6 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
throw new ArgumentNullException(nameof(actionContext));
}
- if (modelState == null)
- {
- throw new ArgumentNullException(nameof(modelState));
- }
-
if (metadataProvider == null)
{
throw new ArgumentNullException(nameof(metadataProvider));
@@ -438,7 +403,6 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
modelType,
prefix,
actionContext,
- modelState,
metadataProvider,
modelBinder,
valueProvider,
@@ -458,8 +422,6 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
/// The prefix to use when looking up values in the .
///
/// The for the current executing request.
- /// The used for maintaining state and
- /// results of model-binding validation.
/// The provider used for reading metadata for the model type.
/// The used for binding.
/// The used for looking up values.
@@ -478,7 +440,6 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
Type modelType,
string prefix,
ActionContext actionContext,
- ModelStateDictionary modelState,
IModelMetadataProvider metadataProvider,
IModelBinder modelBinder,
IValueProvider valueProvider,
@@ -507,11 +468,6 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
throw new ArgumentNullException(nameof(actionContext));
}
- if (modelState == null)
- {
- throw new ArgumentNullException(nameof(modelState));
- }
-
if (metadataProvider == null)
{
throw new ArgumentNullException(nameof(metadataProvider));
@@ -558,6 +514,7 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
var modelMetadata = metadataProvider.GetMetadataForType(modelType);
// Clear ModelStateDictionary entries for the model so that it will be re-validated.
+ var modelState = actionContext.ModelState;
ClearValidationStateForModel(modelType, modelState, metadataProvider, prefix);
var operationBindingContext = new OperationBindingContext
@@ -572,7 +529,6 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
var modelBindingContext = ModelBindingContext.CreateBindingContext(
operationBindingContext,
- modelState,
modelMetadata,
bindingInfo: null,
modelName: prefix ?? string.Empty);
@@ -583,8 +539,8 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
if (modelBindingResult.IsModelSet)
{
objectModelValidator.Validate(
+ operationBindingContext.ActionContext,
operationBindingContext.ValidatorProvider,
- modelState,
modelBindingContext.ValidationState,
modelBindingResult.Key,
modelBindingResult.Model);
diff --git a/src/Microsoft.AspNet.Mvc.Core/ModelBinding/Validation/DefaultObjectValidator.cs b/src/Microsoft.AspNet.Mvc.Core/ModelBinding/Validation/DefaultObjectValidator.cs
index 6e090cea58..5ea43b5d95 100644
--- a/src/Microsoft.AspNet.Mvc.Core/ModelBinding/Validation/DefaultObjectValidator.cs
+++ b/src/Microsoft.AspNet.Mvc.Core/ModelBinding/Validation/DefaultObjectValidator.cs
@@ -40,26 +40,26 @@ namespace Microsoft.AspNet.Mvc.ModelBinding.Validation
///
public void Validate(
+ ActionContext actionContext,
IModelValidatorProvider validatorProvider,
- ModelStateDictionary modelState,
ValidationStateDictionary validationState,
string prefix,
object model)
{
+ if (actionContext == null)
+ {
+ throw new ArgumentNullException(nameof(actionContext));
+ }
+
if (validatorProvider == null)
{
throw new ArgumentNullException(nameof(validatorProvider));
}
- if (modelState == null)
- {
- throw new ArgumentNullException(nameof(modelState));
- }
-
var visitor = new ValidationVisitor(
+ actionContext,
validatorProvider,
_excludeFilters,
- modelState,
validationState);
var metadata = model == null ? null : _modelMetadataProvider.GetMetadataForType(model.GetType());
diff --git a/src/Microsoft.AspNet.Mvc.Core/ModelBinding/Validation/IObjectModelValidator.cs b/src/Microsoft.AspNet.Mvc.Core/ModelBinding/Validation/IObjectModelValidator.cs
index 502c5beab6..9f4702950a 100644
--- a/src/Microsoft.AspNet.Mvc.Core/ModelBinding/Validation/IObjectModelValidator.cs
+++ b/src/Microsoft.AspNet.Mvc.Core/ModelBinding/Validation/IObjectModelValidator.cs
@@ -1,8 +1,6 @@
// 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.
-using Microsoft.Extensions.Internal;
-
namespace Microsoft.AspNet.Mvc.ModelBinding.Validation
{
///
@@ -13,16 +11,16 @@ namespace Microsoft.AspNet.Mvc.ModelBinding.Validation
///
/// Validates the provided object.
///
+ /// The associated with the current request.
/// The .
- /// The .
/// The . May be null.
///
/// The model prefix. Used to map the model object to entries in .
///
/// The model object.
void Validate(
+ ActionContext actionContext,
IModelValidatorProvider validatorProvider,
- ModelStateDictionary modelState,
ValidationStateDictionary validationState,
string prefix,
object model);
diff --git a/src/Microsoft.AspNet.Mvc.Core/ModelBinding/Validation/ValidationVisitor.cs b/src/Microsoft.AspNet.Mvc.Core/ModelBinding/Validation/ValidationVisitor.cs
index e232caad79..3956b60314 100644
--- a/src/Microsoft.AspNet.Mvc.Core/ModelBinding/Validation/ValidationVisitor.cs
+++ b/src/Microsoft.AspNet.Mvc.Core/ModelBinding/Validation/ValidationVisitor.cs
@@ -15,6 +15,7 @@ namespace Microsoft.AspNet.Mvc.ModelBinding.Validation
{
private readonly IModelValidatorProvider _validatorProvider;
private readonly IList _excludeFilters;
+ private readonly ActionContext _actionContext;
private readonly ModelStateDictionary _modelState;
private readonly ValidationStateDictionary _validationState;
@@ -30,16 +31,21 @@ namespace Microsoft.AspNet.Mvc.ModelBinding.Validation
///
/// Creates a new .
///
+ /// The associated with the current request.
/// The .
/// The list of .
- /// The .
/// The .
public ValidationVisitor(
+ ActionContext actionContext,
IModelValidatorProvider validatorProvider,
IList excludeFilters,
- ModelStateDictionary modelState,
ValidationStateDictionary validationState)
{
+ if (actionContext == null)
+ {
+ throw new ArgumentNullException(nameof(actionContext));
+ }
+
if (validatorProvider == null)
{
throw new ArgumentNullException(nameof(validatorProvider));
@@ -50,16 +56,12 @@ namespace Microsoft.AspNet.Mvc.ModelBinding.Validation
throw new ArgumentNullException(nameof(excludeFilters));
}
- if (modelState == null)
- {
- throw new ArgumentNullException(nameof(modelState));
- }
-
+ _actionContext = actionContext;
_validatorProvider = validatorProvider;
_excludeFilters = excludeFilters;
- _modelState = modelState;
_validationState = validationState;
+ _modelState = actionContext.ModelState;
_currentPath = new HashSet