ModelBindingHelper Class¶
- Namespace
Microsoft.AspNetCore.Mvc.ModelBinding.Internal
- Assemblies
- Microsoft.AspNetCore.Mvc.Core
Syntax¶
public class ModelBindingHelper
-
class
Microsoft.AspNetCore.Mvc.ModelBinding.Internal.
ModelBindingHelper
Methods¶
-
CanGetCompatibleCollection<T>
(Microsoft.AspNetCore.Mvc.ModelBinding.ModelBindingContext)¶ Gets an indication whether
GetCompatibleCollection{T}
is likely to return a usable non-<code>null</code> value.Arguments: bindingContext (Microsoft.AspNetCore.Mvc.ModelBinding.ModelBindingContext) – The Microsoft.AspNetCore.Mvc.ModelBinding.ModelBindingContext
.Return type: System.Boolean Returns: <code>true</code> if GetCompatibleCollection{T}
is likely to return a usable non-<code>null</code> value; <code>false</code> otherwise.public static bool CanGetCompatibleCollection<T>(ModelBindingContext bindingContext)
-
ClearValidationStateForModel
(Microsoft.AspNetCore.Mvc.ModelBinding.ModelMetadata, Microsoft.AspNetCore.Mvc.ModelBinding.ModelStateDictionary, System.String)¶ Clears
Microsoft.AspNetCore.Mvc.ModelBinding.ModelStateDictionary
entries forMicrosoft.AspNetCore.Mvc.ModelBinding.ModelMetadata
.Arguments: - modelMetadata (Microsoft.AspNetCore.Mvc.ModelBinding.ModelMetadata) – The
Microsoft.AspNetCore.Mvc.ModelBinding.ModelMetadata
. - modelState (Microsoft.AspNetCore.Mvc.ModelBinding.ModelStateDictionary) – The
Microsoft.AspNetCore.Mvc.ModelBinding.ModelStateDictionary
associated with the model. - modelKey (System.String) – The entry to clear.
public static void ClearValidationStateForModel(ModelMetadata modelMetadata, ModelStateDictionary modelState, string modelKey)
- modelMetadata (Microsoft.AspNetCore.Mvc.ModelBinding.ModelMetadata) – The
-
ClearValidationStateForModel
(System.Type, Microsoft.AspNetCore.Mvc.ModelBinding.ModelStateDictionary, Microsoft.AspNetCore.Mvc.ModelBinding.IModelMetadataProvider, System.String) Clears
Microsoft.AspNetCore.Mvc.ModelBinding.ModelStateDictionary
entries forMicrosoft.AspNetCore.Mvc.ModelBinding.ModelMetadata
.Arguments: - modelType (System.Type) – The
System.Type
of the model. - modelState (Microsoft.AspNetCore.Mvc.ModelBinding.ModelStateDictionary) – The
Microsoft.AspNetCore.Mvc.ModelBinding.ModelStateDictionary
associated with the model. - metadataProvider (Microsoft.AspNetCore.Mvc.ModelBinding.IModelMetadataProvider) – The
Microsoft.AspNetCore.Mvc.ModelBinding.IModelMetadataProvider
. - modelKey (System.String) – The entry to clear.
public static void ClearValidationStateForModel(Type modelType, ModelStateDictionary modelState, IModelMetadataProvider metadataProvider, string modelKey)
- modelType (System.Type) – The
-
ConvertTo
(System.Object, System.Type)¶ Converts the provided <em>value</em> to a value of
System.Type
<em>type</em> using theSystem.Globalization.CultureInfo.InvariantCulture
.Arguments: - value (System.Object) – The value to convert.
- type (System.Type) – The
System.Type
for conversion.
Return type: System.Object
Returns: The converted value or <code>null</code> if the value could not be converted.
public static object ConvertTo(object value, Type type)
-
ConvertTo
(System.Object, System.Type, System.Globalization.CultureInfo) Converts the provided <em>value</em> to a value of
System.Type
<em>type</em>.Arguments: - value (System.Object) – The value to convert.”/>
- type (System.Type) – The
System.Type
for conversion. - culture (System.Globalization.CultureInfo) – The
System.Globalization.CultureInfo
for conversion.
Return type: System.Object
Returns: The converted value or <code>null</code> if the value could not be converted.
public static object ConvertTo(object value, Type type, CultureInfo culture)
-
ConvertTo<T>
(System.Object)¶ Converts the provided <em>value</em> to a value of
System.Type
<em>T</em> using theSystem.Globalization.CultureInfo.InvariantCulture
.Arguments: value (System.Object) – The value to convert.”/> Return type: T Returns: The converted value or the default value of <em>T</em> if the value could not be converted. public static T ConvertTo<T>(object value)
-
ConvertTo<T>
(System.Object, System.Globalization.CultureInfo) Converts the provided <em>value</em> to a value of
System.Type
<em>T</em>.Arguments: - value (System.Object) – The value to convert.”/>
- culture (System.Globalization.CultureInfo) – The
System.Globalization.CultureInfo
for conversion.
Return type: T
Returns: The converted value or the default value of <em>T</em> if the value could not be converted.
public static T ConvertTo<T>(object value, CultureInfo culture)
-
GetCompatibleCollection<T>
(Microsoft.AspNetCore.Mvc.ModelBinding.ModelBindingContext)¶ Creates an
System.Collections.Generic.ICollection`1
instance compatible with <em>bindingContext</em>’sMicrosoft.AspNetCore.Mvc.ModelBinding.ModelBindingContext.ModelType
.Arguments: bindingContext (Microsoft.AspNetCore.Mvc.ModelBinding.ModelBindingContext) – The Microsoft.AspNetCore.Mvc.ModelBinding.ModelBindingContext
.Return type: System.Collections.Generic.ICollection<T> Returns: An System.Collections.Generic.ICollection`1
instance compatible with <em>bindingContext</em>’sMicrosoft.AspNetCore.Mvc.ModelBinding.ModelBindingContext.ModelType
.public static ICollection<T> GetCompatibleCollection<T>(ModelBindingContext bindingContext)
-
GetCompatibleCollection<T>
(Microsoft.AspNetCore.Mvc.ModelBinding.ModelBindingContext, System.Int32) Creates an
System.Collections.Generic.ICollection`1
instance compatible with <em>bindingContext</em>’sMicrosoft.AspNetCore.Mvc.ModelBinding.ModelBindingContext.ModelType
.Arguments: - bindingContext (Microsoft.AspNetCore.Mvc.ModelBinding.ModelBindingContext) – The
Microsoft.AspNetCore.Mvc.ModelBinding.ModelBindingContext
. - capacity (System.Int32) – Capacity for use when creating a
System.Collections.Generic.List`1
instance. Not used when creating another type.
Return type: System.Collections.Generic.ICollection<T>
Returns: An
System.Collections.Generic.ICollection`1
instance compatible with <em>bindingContext</em>’sMicrosoft.AspNetCore.Mvc.ModelBinding.ModelBindingContext.ModelType
.public static ICollection<T> GetCompatibleCollection<T>(ModelBindingContext bindingContext, int capacity)
- bindingContext (Microsoft.AspNetCore.Mvc.ModelBinding.ModelBindingContext) – The
-
GetPropertyFilterExpression<TModel>
(System.Linq.Expressions.Expression<System.Func<TModel, System.Object>>[])¶ Creates an expression for a predicate to limit the set of properties used in model binding.
Arguments: expressions (System.Linq.Expressions.Expression<System.Linq.Expressions.Expression`1>{System.Func<System.Func`2>{TModel, System.Object<System.Object>}}[]) – Expressions identifying the properties to allow for binding. Return type: System.Linq.Expressions.Expression<System.Func<Microsoft.AspNetCore.Mvc.ModelBinding.ModelMetadata>> Returns: An expression which can be used with Microsoft.AspNetCore.Mvc.ModelBinding.IPropertyFilterProvider
.public static Expression<Func<ModelMetadata, bool>> GetPropertyFilterExpression<TModel>(Expression<Func<TModel, object>>[] expressions)
-
TryUpdateModelAsync
(System.Object, System.Type, System.String, Microsoft.AspNetCore.Mvc.ActionContext, Microsoft.AspNetCore.Mvc.ModelBinding.IModelMetadataProvider, Microsoft.AspNetCore.Mvc.ModelBinding.IModelBinderFactory, Microsoft.AspNetCore.Mvc.ModelBinding.IValueProvider, Microsoft.AspNetCore.Mvc.ModelBinding.Validation.IObjectModelValidator)¶ Updates the specified <em>model</em> instance using the specified <em>modelBinderFactory</em> and the specified <em>valueProvider</em> and executes validation using the specified <em>objectModelValidator</em>.
Arguments: - model (System.Object) – The model instance to update and validate.
- modelType (System.Type) – The type of model instance to update and validate.
- prefix (System.String) – The prefix to use when looking up values in the <em>valueProvider</em>.
- actionContext (Microsoft.AspNetCore.Mvc.ActionContext) – The
Microsoft.AspNetCore.Mvc.ActionContext
for the current executing request. - metadataProvider (Microsoft.AspNetCore.Mvc.ModelBinding.IModelMetadataProvider) – The provider used for reading metadata for the model type.
- modelBinderFactory (Microsoft.AspNetCore.Mvc.ModelBinding.IModelBinderFactory) – The
Microsoft.AspNetCore.Mvc.ModelBinding.IModelBinderFactory
used for binding. - valueProvider (Microsoft.AspNetCore.Mvc.ModelBinding.IValueProvider) – The
Microsoft.AspNetCore.Mvc.ModelBinding.IValueProvider
used for looking up values. - objectModelValidator (Microsoft.AspNetCore.Mvc.ModelBinding.Validation.IObjectModelValidator) – The
Microsoft.AspNetCore.Mvc.ModelBinding.Validation.IObjectModelValidator
used for validating the bound values.
Return type: System.Threading.Tasks.Task<System.Boolean>
Returns: A
System.Threading.Tasks.Task
that on completion returns <code>true</code> if the update is successfulpublic static Task<bool> TryUpdateModelAsync(object model, Type modelType, string prefix, ActionContext actionContext, IModelMetadataProvider metadataProvider, IModelBinderFactory modelBinderFactory, IValueProvider valueProvider, IObjectModelValidator objectModelValidator)
-
TryUpdateModelAsync
(System.Object, System.Type, System.String, Microsoft.AspNetCore.Mvc.ActionContext, Microsoft.AspNetCore.Mvc.ModelBinding.IModelMetadataProvider, Microsoft.AspNetCore.Mvc.ModelBinding.IModelBinderFactory, Microsoft.AspNetCore.Mvc.ModelBinding.IValueProvider, Microsoft.AspNetCore.Mvc.ModelBinding.Validation.IObjectModelValidator, System.Func<Microsoft.AspNetCore.Mvc.ModelBinding.ModelMetadata, System.Boolean>) Updates the specified <em>model</em> instance using the specified <em>modelBinderFactory</em> and the specified <em>valueProvider</em> and executes validation using the specified <em>objectModelValidator</em>.
Arguments: - model (System.Object) – The model instance to update and validate.
- modelType (System.Type) – The type of model instance to update and validate.
- prefix (System.String) – The prefix to use when looking up values in the <em>valueProvider</em>.
- actionContext (Microsoft.AspNetCore.Mvc.ActionContext) – The
Microsoft.AspNetCore.Mvc.ActionContext
for the current executing request. - metadataProvider (Microsoft.AspNetCore.Mvc.ModelBinding.IModelMetadataProvider) – The provider used for reading metadata for the model type.
- modelBinderFactory (Microsoft.AspNetCore.Mvc.ModelBinding.IModelBinderFactory) – The
Microsoft.AspNetCore.Mvc.ModelBinding.IModelBinderFactory
used for binding. - valueProvider (Microsoft.AspNetCore.Mvc.ModelBinding.IValueProvider) – The
Microsoft.AspNetCore.Mvc.ModelBinding.IValueProvider
used for looking up values. - objectModelValidator (Microsoft.AspNetCore.Mvc.ModelBinding.Validation.IObjectModelValidator) – The
Microsoft.AspNetCore.Mvc.ModelBinding.Validation.IObjectModelValidator
used for validating the bound values. - propertyFilter (System.Func<Microsoft.AspNetCore.Mvc.ModelBinding.ModelMetadata>) – A predicate which can be used to filter properties(for inclusion/exclusion) at runtime.
Return type: System.Threading.Tasks.Task<System.Boolean>
Returns: A
System.Threading.Tasks.Task
that on completion returns <code>true</code> if the update is successfulpublic static Task<bool> TryUpdateModelAsync(object model, Type modelType, string prefix, ActionContext actionContext, IModelMetadataProvider metadataProvider, IModelBinderFactory modelBinderFactory, IValueProvider valueProvider, IObjectModelValidator objectModelValidator, Func<ModelMetadata, bool> propertyFilter)
-
TryUpdateModelAsync<TModel>
(TModel, System.String, Microsoft.AspNetCore.Mvc.ActionContext, Microsoft.AspNetCore.Mvc.ModelBinding.IModelMetadataProvider, Microsoft.AspNetCore.Mvc.ModelBinding.IModelBinderFactory, Microsoft.AspNetCore.Mvc.ModelBinding.IValueProvider, Microsoft.AspNetCore.Mvc.ModelBinding.Validation.IObjectModelValidator)¶ Updates the specified <em>model</em> instance using the specified <em>modelBinderFactory</em> and the specified <em>valueProvider</em> and executes validation using the specified <em>objectModelValidator</em>.
Arguments: - model (TModel) – The model instance to update and validate.
- prefix (System.String) – The prefix to use when looking up values in the <em>valueProvider</em>.
- actionContext (Microsoft.AspNetCore.Mvc.ActionContext) – The
Microsoft.AspNetCore.Mvc.ActionContext
for the current executing request. - metadataProvider (Microsoft.AspNetCore.Mvc.ModelBinding.IModelMetadataProvider) – The provider used for reading metadata for the model type.
- modelBinderFactory (Microsoft.AspNetCore.Mvc.ModelBinding.IModelBinderFactory) – The
Microsoft.AspNetCore.Mvc.ModelBinding.IModelBinderFactory
used for binding. - valueProvider (Microsoft.AspNetCore.Mvc.ModelBinding.IValueProvider) – The
Microsoft.AspNetCore.Mvc.ModelBinding.IValueProvider
used for looking up values. - objectModelValidator (Microsoft.AspNetCore.Mvc.ModelBinding.Validation.IObjectModelValidator) – The
Microsoft.AspNetCore.Mvc.ModelBinding.Validation.IObjectModelValidator
used for validating the bound values.
Return type: System.Threading.Tasks.Task<System.Boolean>
Returns: A
System.Threading.Tasks.Task
that on completion returns <code>true</code> if the update is successfulpublic static Task<bool> TryUpdateModelAsync<TModel>(TModel model, string prefix, ActionContext actionContext, IModelMetadataProvider metadataProvider, IModelBinderFactory modelBinderFactory, IValueProvider valueProvider, IObjectModelValidator objectModelValidator)where TModel : class
-
TryUpdateModelAsync<TModel>
(TModel, System.String, Microsoft.AspNetCore.Mvc.ActionContext, Microsoft.AspNetCore.Mvc.ModelBinding.IModelMetadataProvider, Microsoft.AspNetCore.Mvc.ModelBinding.IModelBinderFactory, Microsoft.AspNetCore.Mvc.ModelBinding.IValueProvider, Microsoft.AspNetCore.Mvc.ModelBinding.Validation.IObjectModelValidator, System.Func<Microsoft.AspNetCore.Mvc.ModelBinding.ModelMetadata, System.Boolean>) Updates the specified <em>model</em> instance using the specified <em>modelBinderFactory</em> and the specified <em>valueProvider</em> and executes validation using the specified <em>objectModelValidator</em>.
Arguments: - model (TModel) – The model instance to update and validate.
- prefix (System.String) – The prefix to use when looking up values in the <em>valueProvider</em>.
- actionContext (Microsoft.AspNetCore.Mvc.ActionContext) – The
Microsoft.AspNetCore.Mvc.ActionContext
for the current executing request. - metadataProvider (Microsoft.AspNetCore.Mvc.ModelBinding.IModelMetadataProvider) – The provider used for reading metadata for the model type.
- modelBinderFactory (Microsoft.AspNetCore.Mvc.ModelBinding.IModelBinderFactory) – The
Microsoft.AspNetCore.Mvc.ModelBinding.IModelBinderFactory
used for binding. - valueProvider (Microsoft.AspNetCore.Mvc.ModelBinding.IValueProvider) – The
Microsoft.AspNetCore.Mvc.ModelBinding.IValueProvider
used for looking up values. - objectModelValidator (Microsoft.AspNetCore.Mvc.ModelBinding.Validation.IObjectModelValidator) – The
Microsoft.AspNetCore.Mvc.ModelBinding.Validation.IObjectModelValidator
used for validating the bound values. - propertyFilter (System.Func<Microsoft.AspNetCore.Mvc.ModelBinding.ModelMetadata>) – A predicate which can be used to filter properties(for inclusion/exclusion) at runtime.
Return type: System.Threading.Tasks.Task<System.Boolean>
Returns: A
System.Threading.Tasks.Task
that on completion returns <code>true</code> if the update is successfulpublic static Task<bool> TryUpdateModelAsync<TModel>(TModel model, string prefix, ActionContext actionContext, IModelMetadataProvider metadataProvider, IModelBinderFactory modelBinderFactory, IValueProvider valueProvider, IObjectModelValidator objectModelValidator, Func<ModelMetadata, bool> propertyFilter)where TModel : class
-
TryUpdateModelAsync<TModel>
(TModel, System.String, Microsoft.AspNetCore.Mvc.ActionContext, Microsoft.AspNetCore.Mvc.ModelBinding.IModelMetadataProvider, Microsoft.AspNetCore.Mvc.ModelBinding.IModelBinderFactory, Microsoft.AspNetCore.Mvc.ModelBinding.IValueProvider, Microsoft.AspNetCore.Mvc.ModelBinding.Validation.IObjectModelValidator, System.Linq.Expressions.Expression<System.Func<TModel, System.Object>>[]) Updates the specified <em>model</em> instance using the specified <em>modelBinderFactory</em> and the specified <em>valueProvider</em> and executes validation using the specified <em>objectModelValidator</em>.
Arguments: - model (TModel) – The model instance to update and validate.
- prefix (System.String) – The prefix to use when looking up values in the <em>valueProvider</em>.
- actionContext (Microsoft.AspNetCore.Mvc.ActionContext) – The
Microsoft.AspNetCore.Mvc.ActionContext
for the current executing request. - metadataProvider (Microsoft.AspNetCore.Mvc.ModelBinding.IModelMetadataProvider) – The provider used for reading metadata for the model type.
- modelBinderFactory (Microsoft.AspNetCore.Mvc.ModelBinding.IModelBinderFactory) – The
Microsoft.AspNetCore.Mvc.ModelBinding.IModelBinderFactory
used for binding. - valueProvider (Microsoft.AspNetCore.Mvc.ModelBinding.IValueProvider) – The
Microsoft.AspNetCore.Mvc.ModelBinding.IValueProvider
used for looking up values. - objectModelValidator (Microsoft.AspNetCore.Mvc.ModelBinding.Validation.IObjectModelValidator) – The
Microsoft.AspNetCore.Mvc.ModelBinding.Validation.IObjectModelValidator
used for validating the bound values. - includeExpressions (System.Linq.Expressions.Expression<System.Linq.Expressions.Expression`1>{System.Func<System.Func`2>{TModel, System.Object<System.Object>}}[]) – Expression(s) which represent top level properties which need to be included for the current model.
Return type: System.Threading.Tasks.Task<System.Boolean>
Returns: A
System.Threading.Tasks.Task
that on completion returns <code>true</code> if the update is successfulpublic static Task<bool> TryUpdateModelAsync<TModel>(TModel model, string prefix, ActionContext actionContext, IModelMetadataProvider metadataProvider, IModelBinderFactory modelBinderFactory, IValueProvider valueProvider, IObjectModelValidator objectModelValidator, params Expression<Func<TModel, object>>[] includeExpressions)where TModel : class
-