// 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 System; using Microsoft.AspNetCore.Mvc.ModelBinding.Validation; namespace Microsoft.AspNetCore.Mvc.ModelBinding { /// /// A context that contains operating information for model binding and validation. /// public abstract class ModelBindingContext { /// /// Gets or sets a model name which is explicitly set using an . /// public abstract string BinderModelName { get; set; } /// /// Gets or sets a value which represents the associated with the /// . /// public abstract BindingSource BindingSource { get; set; } /// /// Gets or sets the name of the current field being bound. /// public abstract string FieldName { get; set; } /// /// Gets or sets an indication that the current binder is handling the top-level object. /// /// Passed into the model binding system. public abstract bool IsTopLevelObject { get; set; } /// /// Gets or sets the model value for the current operation. /// /// /// The will typically be set for a binding operation that works /// against a pre-existing model object to update certain properties. /// public abstract object Model { get; set; } /// /// Gets or sets the metadata for the model associated with this context. /// public abstract ModelMetadata ModelMetadata { get; set; } /// /// Gets or sets the name of the model. This property is used as a key for looking up values in /// during model binding. /// public abstract string ModelName { get; set; } /// /// Gets or sets the used to capture values /// for properties in the object graph of the model when binding. /// public abstract ModelStateDictionary ModelState { get; set; } /// /// Gets the type of the model. /// /// /// The property must be set to access this property. /// public abstract Type ModelType { get; } /// /// Represents the associated with this context. /// public abstract OperationBindingContext OperationBindingContext { get; set; } /// /// Gets or sets a predicate which will be evaluated for each property to determine if the property /// is eligible for model binding. /// public abstract Func PropertyFilter { get; set; } /// /// Gets or sets the . Used for tracking validation state to /// customize validation behavior for a model object. /// public abstract ValidationStateDictionary ValidationState { get; set; } /// /// Gets or sets the associated with this context. /// public abstract IValueProvider ValueProvider { get; set; } /// /// /// On completion returns a which /// represents the result of the model binding process. /// /// /// If model binding was successful, the should be a value created /// with . If model binding failed, the /// should be a value created with . /// If there was no data, or this model binder cannot handle the operation, the /// should be null. /// /// public abstract ModelBindingResult? Result { get; set; } /// /// Pushes a layer of state onto this context. Model binders will call this as part of recursion when binding properties /// or collection items. /// /// to assign to the property. /// Name to assign to the property. /// Name to assign to the property. /// Instance to assign to the property. /// A scope object which should be used in a using statement where PushContext is called. public abstract NestedScope EnterNestedScope(ModelMetadata modelMetadata, string fieldName, string modelName, object model); /// /// Pushes a layer of state onto this context. Model binders will call this as part of recursion when binding properties /// or collection items. /// /// A scope object which should be used in a using statement where PushContext is called. public abstract NestedScope EnterNestedScope(); /// /// Removes a layer of state pushed by calling . /// protected abstract void ExitNestedScope(); /// /// Return value of . Should be disposed /// by caller when child binding context state should be popped off of /// the . /// public struct NestedScope : IDisposable { private readonly ModelBindingContext _context; /// /// Initializes the for a . /// /// public NestedScope(ModelBindingContext context) { _context = context; } /// /// Exits the created by calling . /// public void Dispose() { _context.ExitNestedScope(); } } } }