/// <summary> /// Updates a property in the current <see cref="ModelBindingContext.Model"/>. /// </summary> /// <param name="bindingContext">The <see cref="ModelBindingContext"/>.</param> /// <param name="metadata"> /// The <see cref="ModelMetadata"/> for the model containing 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> /// <remarks>Should succeed in all cases that <see cref="CanUpdateProperty"/> returns <c>true</c>.</remarks> protected virtual void SetProperty( ModelBindingContext bindingContext, ModelMetadata metadata, ModelMetadata propertyMetadata, ModelBindingResult result) { if (bindingContext == null) { throw new ArgumentNullException(nameof(bindingContext)); } if (metadata == null) { throw new ArgumentNullException(nameof(metadata)); } if (propertyMetadata == null) { throw new ArgumentNullException(nameof(propertyMetadata)); } var bindingFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.IgnoreCase; var property = bindingContext.ModelType.GetProperty(propertyMetadata.PropertyName, bindingFlags); if (property == null) { // Nothing to do if property does not exist. return; } if (!result.IsModelSet) { // If we don't have a value, don't set it on the model and trounce a pre-initialized value. return; } if (!property.CanWrite) { // Try to handle as a collection if property exists but is not settable. AddToProperty(bindingContext, metadata, property, result); return; } var value = result.Model; try { propertyMetadata.PropertySetter(bindingContext.Model, value); } catch (Exception exception) { AddModelError(exception, bindingContext, result); } }
/// <summary> /// Updates a property in the current <see cref="ModelBindingContext.Model"/>. /// </summary> /// <param name="bindingContext">The <see cref="ModelBindingContext"/>.</param> /// <param name="modelExplorer"> /// The <see cref="ModelExplorer"/> for the model containing property to set. /// </param> /// <param name="propertyMetadata">The <see cref="ModelMetadata"/> for the property to set.</param> /// <param name="dtoResult">The <see cref="ModelBindingResult"/> for the property's new value.</param> /// <param name="requiredValidator"> /// The <see cref="IModelValidator"/> which ensures the value is not <c>null</c>. Or <c>null</c> if no such /// requirement exists. /// </param> /// <remarks>Should succeed in all cases that <see cref="CanUpdateProperty"/> returns <c>true</c>.</remarks> protected virtual void SetProperty( [NotNull] ModelBindingContext bindingContext, [NotNull] ModelExplorer modelExplorer, [NotNull] ModelMetadata propertyMetadata, [NotNull] ModelBindingResult dtoResult) { var bindingFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.IgnoreCase; var property = bindingContext.ModelType.GetProperty( propertyMetadata.PropertyName, bindingFlags); if (property == null) { // Nothing to do if property does not exist. return; } if (!property.CanWrite) { // Try to handle as a collection if property exists but is not settable. AddToProperty(bindingContext, modelExplorer, property, dtoResult); return; } object value = null; if (dtoResult.IsModelSet) { value = dtoResult.Model; } if (!dtoResult.IsModelSet) { // If we don't have a value, don't set it on the model and trounce a pre-initialized // value. return; } try { propertyMetadata.PropertySetter(bindingContext.Model, value); } catch (Exception exception) { AddModelError(exception, bindingContext, dtoResult); } }