private void ApplyPaginationAttributes(TagHelperContext context) { //not specified any pager parameters if (!context.AllAttributes.ContainsName(PageIndexAttributeName) && !context.AllAttributes.ContainsName(PageSizeAttributeName) && !context.AllAttributes.ContainsName(TotalAttributeName)) { //get the model otherwise use the view model ModelExplorer explorer = For?.ModelExplorer; if (explorer == null) { explorer = ViewContext.ViewData.ModelExplorer; } //get the paging values from the model (support the popular pager paramter names) var index = explorer.GetExplorerForProperty(PossiblePageIndexParameterNames); var size = explorer.GetExplorerForProperty(PossiblePageSizeParameterNames); var total = explorer.GetExplorerForProperty(PossibleTotalParameterNames); if (index == null || size == null || total == null) { throw new ArgumentException($"A model MUST contain values for a page-index, page-size and total."); } PageIndex = Convert.ToInt32(index.Model); PageSize = Convert.ToInt32(size.Model); Total = Convert.ToInt32(total.Model); } }
/// <summary> /// If the model derives from <see cref="ActionExecutionModel"/>, enrich the metadata before rendering the editor, else /// use the default metadata. /// </summary> protected override IHtmlContent GenerateEditor(ModelExplorer modelExplorer, string htmlFieldName, string templateName, object additionalViewData) { if (modelExplorer == null) { throw new ArgumentNullException(nameof(modelExplorer)); } if (htmlFieldName != null && modelExplorer.Container != null && typeof(ActionExecutionModel).IsAssignableFrom(modelExplorer.Container.ModelType)) { var actionExecutionModel = (ActionExecutionModel)modelExplorer.Container.Model; var compositeMetadataDetailsProvider = this.ViewContext.HttpContext.RequestServices.GetService <ICompositeMetadataDetailsProvider>(); var actionExecutionModelMetadata = ActionExecutionModelMetadataFactory.GetMetadata(this.MetadataProvider, compositeMetadataDetailsProvider, actionExecutionModel); var actionExecutionModelExplorer = new ModelExplorer(this.MetadataProvider, modelExplorer.Container, actionExecutionModelMetadata, actionExecutionModel); var parameterExplorer = actionExecutionModelExplorer.GetExplorerForProperty(htmlFieldName); return(base.GenerateEditor(parameterExplorer, htmlFieldName, templateName, additionalViewData)); } return(base.GenerateEditor(modelExplorer, htmlFieldName, templateName, additionalViewData)); }
private bool ValidateProperties( string currentModelKey, ModelExplorer modelExplorer, ValidationContext validationContext) { var isValid = true; foreach (var property in modelExplorer.Metadata.Properties) { var propertyExplorer = modelExplorer.GetExplorerForProperty(property.PropertyName); var propertyMetadata = propertyExplorer.Metadata; var propertyValidationContext = new ValidationContext() { ModelValidationContext = ModelValidationContext.GetChildValidationContext( validationContext.ModelValidationContext, propertyExplorer), Visited = validationContext.Visited }; var propertyBindingName = propertyMetadata.BinderModelName ?? propertyMetadata.PropertyName; var childKey = ModelBindingHelper.CreatePropertyModelName(currentModelKey, propertyBindingName); if (!ValidateNonVisitedNodeAndChildren( childKey, propertyValidationContext, validators: null)) { isValid = false; } } return(isValid); }
private IList <ModelValidationNode> GetChildNodes(ValidationContext context, ModelExplorer modelExplorer) { var validationNode = context.ValidationNode; // This is the trivial case where the node-tree that was built-up during binding already has // all of the nodes we need. if (validationNode.ChildNodes.Count != 0 || !validationNode.ValidateAllProperties || validationNode.Model == null) { return(validationNode.ChildNodes); } var childNodes = new List <ModelValidationNode>(validationNode.ChildNodes); var elementMetadata = modelExplorer.Metadata.ElementMetadata; if (elementMetadata == null) { foreach (var property in validationNode.ModelMetadata.Properties) { var propertyExplorer = modelExplorer.GetExplorerForProperty(property.PropertyName); var propertyBindingName = property.BinderModelName ?? property.PropertyName; var childKey = ModelNames.CreatePropertyModelName(validationNode.Key, propertyBindingName); var childNode = new ModelValidationNode(childKey, property, propertyExplorer.Model) { ValidateAllProperties = true }; childNodes.Add(childNode); } } else { var enumerableModel = (IEnumerable)modelExplorer.Model; // An integer index is incorrect in scenarios where there is a custom index provided by the user. // However those scenarios are supported by createing a ModelValidationNode with the right keys. var index = 0; foreach (var element in enumerableModel) { var elementExplorer = new ModelExplorer(_modelMetadataProvider, elementMetadata, element); var elementKey = ModelNames.CreateIndexModelName(validationNode.Key, index); var childNode = new ModelValidationNode(elementKey, elementMetadata, elementExplorer.Model) { ValidateAllProperties = true }; childNodes.Add(childNode); index++; } } return(childNodes); }
public static ModelExplorer GetExplorerForProperty(this ModelExplorer @this, string[] properties) { foreach (var property in properties) { var propertyExplorer = @this.GetExplorerForProperty(property); if (propertyExplorer != null) { return(propertyExplorer); } } return(null); }
private void ExpandValidationNode(ValidationContext context, ModelExplorer modelExplorer) { var validationNode = context.ValidationNode; if (validationNode.ChildNodes.Count != 0 || !validationNode.ValidateAllProperties || validationNode.Model == null) { return; } var elementMetadata = modelExplorer.Metadata.ElementMetadata; if (elementMetadata == null) { foreach (var property in validationNode.ModelMetadata.Properties) { var propertyExplorer = modelExplorer.GetExplorerForProperty(property.PropertyName); var propertyBindingName = property.BinderModelName ?? property.PropertyName; var childKey = ModelNames.CreatePropertyModelName(validationNode.Key, propertyBindingName); var childNode = new ModelValidationNode(childKey, property, propertyExplorer.Model) { ValidateAllProperties = true }; validationNode.ChildNodes.Add(childNode); } } else { var enumerableModel = (IEnumerable)modelExplorer.Model; // An integer index is incorrect in scenarios where there is a custom index provided by the user. // However those scenarios are supported by createing a ModelValidationNode with the right keys. var index = 0; foreach (var element in enumerableModel) { var elementExplorer = new ModelExplorer(_modelMetadataProvider, elementMetadata, element); var elementKey = ModelNames.CreateIndexModelName(validationNode.Key, index); var childNode = new ModelValidationNode(elementKey, elementMetadata, elementExplorer.Model) { ValidateAllProperties = true }; validationNode.ChildNodes.Add(childNode); index++; } } }
private bool ValidateChildNodes( string currentModelKey, ModelExplorer modelExplorer, ValidationContext validationContext) { var isValid = true; ExpandValidationNode(validationContext, modelExplorer); IList <IModelValidator> validators = null; if (modelExplorer.Metadata.IsCollectionType && modelExplorer.Model != null) { var enumerableModel = (IEnumerable)modelExplorer.Model; var elementType = GetElementType(enumerableModel.GetType()); var elementMetadata = _modelMetadataProvider.GetMetadataForType(elementType); validators = GetValidators(validationContext.ModelValidationContext.ValidatorProvider, elementMetadata); } foreach (var childNode in validationContext.ValidationNode.ChildNodes) { var childModelExplorer = childNode.ModelMetadata.MetadataKind == Metadata.ModelMetadataKind.Type ? _modelMetadataProvider.GetModelExplorerForType(childNode.ModelMetadata.ModelType, childNode.Model) : modelExplorer.GetExplorerForProperty(childNode.ModelMetadata.PropertyName); var propertyValidationContext = new ValidationContext() { ModelValidationContext = ModelValidationContext.GetChildValidationContext( validationContext.ModelValidationContext, childModelExplorer), Visited = validationContext.Visited, ValidationNode = childNode }; if (!ValidateNonVisitedNodeAndChildren( childNode.Key, propertyValidationContext, validators)) { isValid = false; } } return(isValid); }
private bool ValidateProperties( string currentModelKey, ModelExplorer modelExplorer, ValidationContext validationContext) { var isValid = true; foreach (var property in modelExplorer.Metadata.Properties) { var propertyExplorer = modelExplorer.GetExplorerForProperty(property.PropertyName); var propertyMetadata = propertyExplorer.Metadata; var propertyValidationContext = new ValidationContext() { ModelValidationContext = ModelValidationContext.GetChildValidationContext( validationContext.ModelValidationContext, propertyExplorer), Visited = validationContext.Visited }; var propertyBindingName = propertyMetadata.BinderModelName ?? propertyMetadata.PropertyName; var childKey = ModelBindingHelper.CreatePropertyModelName(currentModelKey, propertyBindingName); if (!ValidateNonVisitedNodeAndChildren( childKey, propertyValidationContext, validators: null)) { isValid = false; } } return isValid; }
/// <summary> /// Gets a simple display string for the <see cref="ModelExplorer.Model"/> property /// of <paramref name="modelExplorer"/>. /// </summary> /// <param name="modelExplorer">The <see cref="ModelExplorer"/>.</param> /// <returns>A simple display string for the model.</returns> public static string GetSimpleDisplayText(this ModelExplorer modelExplorer) { if (modelExplorer == null) { throw new ArgumentNullException(nameof(modelExplorer)); } if (modelExplorer.Metadata.SimpleDisplayProperty != null) { var propertyExplorer = modelExplorer.GetExplorerForProperty( modelExplorer.Metadata.SimpleDisplayProperty); if (propertyExplorer?.Model != null) { return(propertyExplorer.Model.ToString()); } } if (modelExplorer.Model == null) { return(modelExplorer.Metadata.NullDisplayText); } if (modelExplorer.Metadata.IsEnum && modelExplorer.Model is Enum modelEnum) { var enumStringValue = modelEnum.ToString("d"); var enumGroupedDisplayNamesAndValues = modelExplorer.Metadata.EnumGroupedDisplayNamesAndValues; Debug.Assert(enumGroupedDisplayNamesAndValues != null); foreach (var kvp in enumGroupedDisplayNamesAndValues) { if (string.Equals(kvp.Value, enumStringValue, StringComparison.Ordinal)) { return(kvp.Key.Name); } } } var stringResult = Convert.ToString(modelExplorer.Model, CultureInfo.CurrentCulture); if (stringResult == null) { return(string.Empty); } if (!stringResult.Equals(modelExplorer.Model.GetType().FullName, StringComparison.Ordinal)) { return(stringResult); } if (modelExplorer.PropertiesInternal.Length == 0) { return(string.Empty); } var firstProperty = modelExplorer.PropertiesInternal[0]; if (firstProperty.Model == null) { return(firstProperty.Metadata.NullDisplayText); } return(Convert.ToString(firstProperty.Model, CultureInfo.CurrentCulture)); }