public async Task <bool> BindModelAsync(ModelBindingContext bindingContext)
        {
            ModelBindingHelper.ValidateBindingContext(bindingContext);

            if (!ValueProviderResult.CanConvertFromString(bindingContext.ModelType))
            {
                // this type cannot be converted
                return(false);
            }

            var valueProviderResult = await bindingContext.ValueProvider.GetValueAsync(bindingContext.ModelName);

            if (valueProviderResult == null)
            {
                return(false); // no entry
            }

            object newModel;

            bindingContext.ModelState.SetModelValue(bindingContext.ModelName, valueProviderResult);
            try
            {
                newModel = valueProviderResult.ConvertTo(bindingContext.ModelType);
                ModelBindingHelper.ReplaceEmptyStringWithNull(bindingContext.ModelMetadata, ref newModel);
                bindingContext.Model = newModel;
            }
            catch (Exception ex)
            {
                ModelBindingHelper.AddModelErrorBasedOnExceptionType(bindingContext, ex);
            }

            return(true);
        }
Beispiel #2
0
        private static bool CanBindType(Type modelType)
        {
            // Simple types cannot use this binder
            var isComplexType = !ValueProviderResult.CanConvertFromString(modelType);

            if (!isComplexType)
            {
                return(false);
            }

            if (modelType == typeof(ComplexModelDto))
            {
                // forbidden type - will cause a stack overflow if we try binding this type
                return(false);
            }
            return(true);
        }