Example #1
0
        /// <summary>
        /// Create a scope for a child model
        /// </summary>
        /// <param name="type">The type.</param>
        /// <param name="prefix">The prefix.</param>
        /// <param name="modelName">Name of the model.</param>
        /// <returns></returns>
        private IModelBinderContext CreateForChild(Type type, string prefix, string modelName)
        {
            var context = new ModelBinderContext(type, modelName, prefix, ValueProvider);

            context.RootBinder = RootBinder;
            return(context);
        }
Example #2
0
        public object[] Bind(ControllerContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            var action = context.ActionDescriptor.MethodInfo;

            if (action == null)
            {
                throw new ArgumentNullException(nameof(action));
            }

            var parameters = action.GetParameters();

            if (parameters.Length == 0)
            {
                return(null);
            }

            var provider = new RequestValueProvider(context.HttpContext.Request);

            var binders = provider.GetKeys();

            if (parameters.Length != binders.Count)
            {
                return(null);
            }

            object[] results = new object[parameters.Length];

            var x = 0;

            foreach (string item in binders)
            {
                var modelContext = new ModelBinderContext(parameters[x].ParameterType, item, string.Empty, provider)
                {
                    RootBinder = this
                };
                foreach (IModelBinder modelBinder in _binders)
                {
                    if (modelBinder.CanBind(modelContext))
                    {
                        results[x] = modelBinder.Bind(modelContext);
                    }
                }
                x++;
            }
            ;

            return(results);
        }