public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { var modelType = ModelBinders.Binders[typeof(Type)].BindModel(controllerContext, bindingContext) as Type; var index = int.Parse(controllerContext.RouteData.Values["index"].ToString()); var actionName = (string)controllerContext.RouteData.Values["action"]; var mapping = GetMethodMapping(controllerContext, modelType, actionName, index); var method = new Method(new MethodDescriptor(mapping, controllerContext.GetActionDescriptor(actionName))); foreach (var parameter in method.Parameters) { var result = bindingContext.ValueProvider.GetValue(parameter.Name); if (result != null) { if (!parameter.IsModel) parameter.Value = result.ConvertTo(parameter.MemberType); else { var context = ModelAssemblies.GetContext(parameter.MemberType); var descriptor = new ModelDescriptor(ModelMappingManager.FindByType(parameter.MemberType)); var value = result.ConvertTo(descriptor.KeyProperty.PropertyType); parameter.Value = context.Set(parameter.MemberType).Find(value); } } } return method; }
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; }