/// <summary> /// Initializes a new instance of the <see cref="ViewDataDictionary"/> class. /// </summary> /// <param name="metadataProvider"> /// <see cref = "IModelMetadataProvider" /> instance used to calculate /// <see cref="ViewDataDictionary.ModelMetadata"/> values. /// </param> /// <param name="modelState"><see cref="ModelStateDictionary"/> instance for this scope.</param> /// <param name="declaredModelType"> /// <see cref="Type"/> of <see cref="Model"/> values expected. Used to set /// <see cref="ViewDataDictionary.ModelMetadata"/> when <see cref="Model"/> is <c>null</c>. /// </param> /// <remarks> /// For use when creating a derived <see cref="ViewDataDictionary"/> for a new top-level scope. /// </remarks> protected ViewDataDictionary( IModelMetadataProvider metadataProvider, ModelStateDictionary modelState, Type declaredModelType) : this(metadataProvider, modelState, declaredModelType, data : new Dictionary <string, object>(StringComparer.OrdinalIgnoreCase), templateInfo : new TemplateInfo()) { if (metadataProvider == null) { throw new ArgumentNullException(nameof(metadataProvider)); } if (modelState == null) { throw new ArgumentNullException(nameof(modelState)); } if (declaredModelType == null) { throw new ArgumentNullException(nameof(declaredModelType)); } // This is the core constructor called when Model is unknown. Base ModelMetadata on the declared type. ModelExplorer = _metadataProvider.GetModelExplorerForType(declaredModelType, model: null); }
/// <summary> /// Initializes a new instance of the <see cref="ViewDataDictionary"/> class. /// </summary> /// <param name="metadataProvider"> /// <see cref = "IModelMetadataProvider" /> instance used to calculate /// <see cref="ViewDataDictionary.ModelMetadata"/> values. /// </param> /// <param name="modelState"><see cref="ModelStateDictionary"/> instance for this scope.</param> /// <param name="declaredModelType"> /// <see cref="Type"/> of <see cref="Model"/> values expected. Used to set /// <see cref="ViewDataDictionary.ModelMetadata"/> when <see cref="Model"/> is <c>null</c>. /// </param> /// <remarks> /// For use when creating a derived <see cref="ViewDataDictionary"/> for a new top-level scope. /// </remarks> protected ViewDataDictionary( [NotNull] IModelMetadataProvider metadataProvider, [NotNull] ModelStateDictionary modelState, [NotNull] Type declaredModelType) : this(metadataProvider, modelState, declaredModelType, data : new Dictionary <string, object>(StringComparer.OrdinalIgnoreCase), templateInfo : new TemplateInfo()) { // This is the core constructor called when Model is unknown. Base ModelMetadata on the declared type. ModelExplorer = _metadataProvider.GetModelExplorerForType(declaredModelType, model : null); }
/// <summary> /// Gets <see cref="ModelExplorer"/> for named <paramref name="expression"/> in given /// <paramref name="viewData"/>. /// </summary> /// <param name="expression">Expression name, relative to <c>viewData.Model</c>.</param> /// <param name="viewData"> /// The <see cref="ViewDataDictionary"/> that may contain the <paramref name="expression"/> value. /// </param> /// <param name="metadataProvider">The <see cref="IModelMetadataProvider"/>.</param> /// <returns> /// <see cref="ModelExplorer"/> for named <paramref name="expression"/> in given <paramref name="viewData"/>. /// </returns> public static ModelExplorer FromStringExpression( string expression, [NotNull] ViewDataDictionary viewData, IModelMetadataProvider metadataProvider) { var viewDataInfo = ViewDataEvaluator.Eval(viewData, expression); if (viewDataInfo == null) { // Try getting a property from ModelMetadata if we couldn't find an answer in ViewData var propertyExplorer = viewData.ModelExplorer.GetExplorerForProperty(expression); if (propertyExplorer != null) { return(propertyExplorer); } } if (viewDataInfo != null) { if (viewDataInfo.Container == viewData && viewDataInfo.Value == viewData.Model && string.IsNullOrEmpty(expression)) { // Nothing for empty expression in ViewData and ViewDataEvaluator just returned the model. Handle // using FromModel() for its object special case. return(FromModel(viewData, metadataProvider)); } ModelExplorer containerExplorer = viewData.ModelExplorer; if (viewDataInfo.Container != null) { containerExplorer = metadataProvider.GetModelExplorerForType( viewDataInfo.Container.GetType(), viewDataInfo.Container); } if (viewDataInfo.PropertyInfo != null) { // We've identified a property access, which provides us with accurate metadata. var containerType = viewDataInfo.Container?.GetType() ?? viewDataInfo.PropertyInfo.DeclaringType; var containerMetadata = metadataProvider.GetMetadataForType(viewDataInfo.Container.GetType()); var propertyMetadata = containerMetadata.Properties[viewDataInfo.PropertyInfo.Name]; Func <object, object> modelAccessor = (ignore) => viewDataInfo.Value; return(containerExplorer.GetExplorerForExpression(propertyMetadata, modelAccessor)); } else if (viewDataInfo.Value != null) { // We have a value, even though we may not know where it came from. var valueMetadata = metadataProvider.GetMetadataForType(viewDataInfo.Value.GetType()); return(containerExplorer.GetExplorerForExpression(valueMetadata, viewDataInfo.Value)); } } // Treat the expression as string if we don't find anything better. var stringMetadata = metadataProvider.GetMetadataForType(typeof(string)); return(viewData.ModelExplorer.GetExplorerForExpression(stringMetadata, modelAccessor: null)); }
private ModelExplorer GetModelExplorer(Type modelType) { if (this.Model != null) { return(_modelMetadataProvider.GetModelExplorerForType(this.Model.GetType(), Model)); /// ModelType, null); } return(null); }
private ModelExplorer GetModelExplorer(Type modelType) { if (modelType != null) { return(_modelMetadataProvider.GetModelExplorerForType(ModelType, null)); } return(null); }
private static ModelExplorer FromModel(ViewDataDictionary data, IModelMetadataProvider provider) { if (data.ModelMetadata.ModelType == typeof(Object)) { return(provider.GetModelExplorerForType(typeof(String), data.Model ?? Convert.ToString(data.Model, CultureInfo.CurrentCulture))); } return(data.ModelExplorer); }
private static ModelExplorer FromModel(ViewDataDictionary viewData, IModelMetadataProvider metadataProvider) { if (viewData.ModelMetadata.ModelType == typeof(object)) { // Use common simple type rather than object so e.g. Editor() at least generates a TextBox. var model = viewData.Model is null ? null : Convert.ToString(viewData.Model, CultureInfo.CurrentCulture); return(metadataProvider.GetModelExplorerForType(typeof(string), model)); } else { return(viewData.ModelExplorer); } }