Ejemplo n.º 1
0
 public Task BindAsync(ModelBindingContext modelBindingContext)
 {
     if (modelBindingContext.ValueProvider.TryGetValues(modelBindingContext.ModelName, out var values))
     {
         var value = Convert.ChangeType(values.Last(), modelBindingContext.ModelMetadata.ModelType);
         modelBindingContext.Bind(value);
     }
     // modelBindingContext.Bind(method.MakeGenericMethod(modelBindingContext.ModelMetadata.ModelType).Invoke(null, new object[0]));
     return(Task.CompletedTask);
 }
Ejemplo n.º 2
0
        public async Task BindAsync(ModelBindingContext modelBindingContext)
        {
            var metaData = modelBindingContext.ModelMetadata;
            var model    = Activator.CreateInstance(metaData.ModelType);

            foreach (var property in metaData.ModelType.GetProperties().Where(p => p.SetMethod != null))
            {
                var propertyMetaData = ModelMetadata.Create(property);
                var binderFactory    = modelBindingContext.ActionContext.HttpContext.RequestServices.GetRequiredService <IModelBinderFactory>();
                var binder           = binderFactory.CreateModelBinder(propertyMetaData);
                var modelName        = string.IsNullOrWhiteSpace(modelBindingContext.ModelName)
                    ? property.Name
                    : $"{metaData.ModelName}.{property.Name}";
                var propertyContext = new ModelBindingContext(modelBindingContext.ActionContext, propertyMetaData, modelBindingContext.ValueProvider, modelName);
                await binder.BindAsync(propertyContext);

                if (propertyContext.IsModelSet)
                {
                    property.SetValue(model, propertyContext.Model);
                }
            }
            modelBindingContext.Bind(model);
        }
Ejemplo n.º 3
0
        public Task BindAsync(ModelBindingContext modelBindingContext)
        {
            IList array = null;

            if (modelBindingContext.ValueProvider.TryGetValues(modelBindingContext.ModelMetadata.ModelName, out var values))
            {
                Type elementType = modelBindingContext.ModelMetadata.ModelType.GetElementType();

                if (values.Length == 1)
                {
                    var list = (object[])JsonSerializer.Deserialize(values.Last(),
                                                                    modelBindingContext.ModelMetadata.ModelType);
                    if (list != null && list.Length > 0)
                    {
                        array = Array.CreateInstance(elementType, list.Length);
                        for (int i = 0; i < list.Length; i++)
                        {
                            var convertResult = Convert.ChangeType(list.GetValue(i), elementType);
                            array[i] = convertResult;
                        }
                    }
                }
                else
                {
                    array = Array.CreateInstance(elementType, values.Length);
                    for (int i = 0; i < values.Length; i++)
                    {
                        var convertResult = Convert.ChangeType(values.GetValue(i), elementType);
                        array[i] = convertResult;
                    }
                }
            }

            modelBindingContext.Bind(array);

            return(Task.CompletedTask);
        }
Ejemplo n.º 4
0
        public Task BindAsync(ModelBindingContext modelBindingContext)
        {
            IList list = null;

            if (modelBindingContext.ValueProvider.TryGetValues(modelBindingContext.ModelMetadata.ModelName, out var values))
            {
                var array = (IEnumerable <object>)JsonSerializer.Deserialize(values.Last(), modelBindingContext.ModelMetadata.ModelType);

                Type elementType = modelBindingContext.ModelMetadata.ModelType.GetGenericArguments()[0];

                if (array != null && array.Count() > 0)
                {
                    list = Array.CreateInstance(elementType, array.Count());
                    for (int i = 0; i < array.Count(); i++)
                    {
                        var convertResult = Convert.ChangeType(array.ToArray()[i], elementType);
                        list[i] = convertResult;
                    }
                }
            }
            modelBindingContext.Bind(list);

            return(Task.CompletedTask);
        }