Exemplo n.º 1
0
        public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            var actionName = (string)controllerContext.RouteData.Values["action"];
            var mapping = GetMethodMapping(controllerContext, bindingContext, actionName);

            var method = new Method(new MethodDescriptor(mapping, controllerContext.GetActionDescriptor(actionName)));

            foreach (var parameter in method.Parameters)
            {
                ValueProviderResult result;
                if (parameter.IsModelCollection)
                {
                    var type = typeof(List<>).MakeGenericType(parameter.UnderliningModel.ModelType);
                    var collection = Activator.CreateInstance(type);
                    var index = 0;
                    while ((result = bindingContext.ValueProvider.GetValue(string.Format("{0}[{1}]", parameter.Name, index))) != null)
                    {
                        var item = ModelBinder.GetModelValue(result, parameter.UnderliningModel.ModelType) ?? Activator.CreateInstance(parameter.UnderliningModel.ModelType);
                        var itemModel = new Model(parameter.UnderliningModel.ModelType, parameter.UnderliningModel.Descriptor, item);
                        foreach (var itemProperty in itemModel.Properties)
                        {
                            var itemPropertyName = string.Format("{0}[{1}].{2}", parameter.Name, index, itemProperty.Name);
                            var itemResult = bindingContext.ValueProvider.GetValue(itemPropertyName);
                            if (itemResult != null)
                            {
                                itemProperty.Value = !itemProperty.IsModel
                                    ? ModelBinder.GetNonModelValue(itemResult, itemProperty.MemberType)
                                    : ModelBinder.GetModelValue(itemResult, itemProperty.MemberType);
                            }
                        }
                        type.GetMethod("Add").Invoke(collection, new[] { item });
                        index++;
                    }
                    parameter.Value = collection;
                }
                else
                {
                    result = bindingContext.ValueProvider.GetValue(parameter.Name);

                    if (result == null)
                    {
                        controllerContext.HttpContext.Request.InputStream.Position = 0;
                        var provider = new JsonValueProviderFactory().GetValueProvider(controllerContext);
                        if (provider != null)
                            result = provider.GetValue(parameter.Name);
                    }

                    if (result != null)
                    {
                        parameter.Value = !parameter.IsModel
                            ? ModelBinder.GetNonModelValue(result, parameter.MemberType)
                            : ModelBinder.GetModelValue(result, parameter.MemberType);
                    }
                }
            }
            return method;
        }
Exemplo n.º 2
0
        public ModelCollection(Type modelType, ModelDescriptor descriptor, IEnumerable items)
        {
            if (modelType == null)
                throw new ArgumentNullException("modelType");
            if (descriptor == null)
                throw new ArgumentNullException("descriptor");
            ModelType = modelType;
            Descriptor = descriptor;

            if (items != null)
                foreach (var item in items)
                    this.items.Add(new Model(modelType, descriptor, item));

            var emptyModel = new Model(modelType, descriptor);

            foreach (var property in descriptor.Properties)
                properties.Add(property.AsModel(emptyModel));
        }