/// <exclude/>
 private static string Replace(this string src, string mask, ModelExplorer explorer)
 {
     return Regex.Replace(WebUtility.UrlDecode(src), mask, (m) =>
     {
         var key = m.Groups[1].Value;
         ModelExplorer property = explorer.GetExplorerForProperty(key);
         return (property?.Model?.ToString() ?? m.Groups[0].Value);
     });
 }
        private void AddToProperty(
            ModelBindingContext bindingContext,
            ModelExplorer modelExplorer,
            PropertyInfo property,
            ModelBindingResult result)
        {
            var propertyExplorer = modelExplorer.GetExplorerForProperty(property.Name);

            var target = propertyExplorer.Model;
            var source = result.Model;

            if (target == null || source == null)
            {
                // Cannot copy to or from a null collection.
                return;
            }

            if (target == source)
            {
                // Added to the target collection in BindPropertiesAsync().
                return;
            }

            // Determine T if this is an ICollection<T> property. No need for a T[] case because CanUpdateProperty()
            // ensures property is either settable or not an array. Underlying assumption is that CanUpdateProperty()
            // and SetProperty() are overridden together.
            var collectionTypeArguments = ClosedGenericMatcher.ExtractGenericInterface(
                propertyExplorer.ModelType,
                typeof(ICollection <>))
                                          ?.GenericTypeArguments;

            if (collectionTypeArguments == null)
            {
                // Not a collection model.
                return;
            }

            var propertyAddRange = CallPropertyAddRangeOpenGenericMethod.MakeGenericMethod(collectionTypeArguments);

            try
            {
                propertyAddRange.Invoke(obj: null, parameters: new[] { target, source });
            }
            catch (Exception exception)
            {
                AddModelError(exception, bindingContext, result);
            }
        }
Example #3
0
        /// <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([NotNull] this ModelExplorer 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);
            }

            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);
            }

            var firstProperty = modelExplorer.Properties.FirstOrDefault();

            if (firstProperty == null)
            {
                return(string.Empty);
            }

            if (firstProperty.Model == null)
            {
                return(firstProperty.Metadata.NullDisplayText);
            }

            return(Convert.ToString(firstProperty.Model, CultureInfo.CurrentCulture));
        }