1 /* ****************************************************************************
\r
3 * Copyright (c) Microsoft Corporation. All rights reserved.
\r
5 * This software is subject to the Microsoft Public License (Ms-PL).
\r
6 * A copy of the license can be found in the license.htm file included
\r
7 * in this distribution.
\r
9 * You must not remove this notice, or any other, from this software.
\r
11 * ***************************************************************************/
\r
13 namespace System.Web.Mvc {
\r
15 using System.Collections.Generic;
\r
16 using System.Diagnostics.CodeAnalysis;
\r
18 using System.Web.Mvc.Resources;
\r
20 public class ModelBindingContext {
\r
22 private static readonly Predicate<string> _defaultPropertyFilter = _ => true;
\r
24 private string _modelName;
\r
25 private ModelStateDictionary _modelState;
\r
26 private Predicate<string> _propertyFilter;
\r
27 private Dictionary<string, ModelMetadata> _propertyMetadata;
\r
29 public ModelBindingContext()
\r
33 // copies certain values that won't change between parent and child objects,
\r
34 // e.g. ValueProvider, ModelState
\r
35 public ModelBindingContext(ModelBindingContext bindingContext) {
\r
36 if (bindingContext != null) {
\r
37 ModelState = bindingContext.ModelState;
\r
38 ValueProvider = bindingContext.ValueProvider;
\r
42 public bool FallbackToEmptyPrefix {
\r
47 [SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "value", Justification = "Cannot remove setter as that's a breaking change")]
\r
48 public object Model {
\r
50 return ModelMetadata.Model;
\r
53 throw new InvalidOperationException(MvcResources.ModelMetadata_PropertyNotSettable);
\r
57 public ModelMetadata ModelMetadata {
\r
62 public string ModelName {
\r
64 if (_modelName == null) {
\r
65 _modelName = String.Empty;
\r
74 [SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly", Justification = "The containing type is mutable.")]
\r
75 public ModelStateDictionary ModelState {
\r
77 if (_modelState == null) {
\r
78 _modelState = new ModelStateDictionary();
\r
83 _modelState = value;
\r
87 [SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "value", Justification = "Cannot remove setter as that's a breaking change")]
\r
88 public Type ModelType {
\r
90 return ModelMetadata.ModelType;
\r
93 throw new InvalidOperationException(MvcResources.ModelMetadata_PropertyNotSettable);
\r
97 public Predicate<string> PropertyFilter {
\r
99 if (_propertyFilter == null) {
\r
100 _propertyFilter = _defaultPropertyFilter;
\r
102 return _propertyFilter;
\r
105 _propertyFilter = value;
\r
109 public IDictionary<string, ModelMetadata> PropertyMetadata {
\r
111 if (_propertyMetadata == null) {
\r
112 _propertyMetadata = ModelMetadata.Properties.ToDictionary(m => m.PropertyName, StringComparer.OrdinalIgnoreCase);
\r
115 return _propertyMetadata;
\r
119 public IValueProvider ValueProvider {
\r