Esempio n. 1
0
        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);
        }
Esempio n. 2
0
        public static Query.Query Query(this Member member, ControllerContext controllerContext)
        {
            var context    = ModelAssemblies.GetContext(member.MemberType);
            var actionName = controllerContext.RouteData.Values["action"].ToString();
            var action     = (RunningObjectsAction)Enum.Parse(typeof(RunningObjectsAction), actionName);
            var source     = context.Set(member.MemberType);
            var attr       = member.Attributes.OfType <QueryAttribute>().FirstOrDefault();

            return(attr != null
                ? QueryParser.Parse(member.MemberType, source, attr)
                : QueryParser.Parse(member.MemberType, source));
        }
Esempio n. 3
0
        public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            var modelType = ModelBinders.Binders[typeof(Type)].BindModel(controllerContext, bindingContext) as Type;

            var key        = controllerContext.RouteData.Values["key"];
            var mapping    = ModelMappingManager.FindByType(modelType);
            var descriptor = new ModelDescriptor(mapping);

            using (var context = ModelAssemblies.GetContext(modelType))
            {
                var set      = context.Set(modelType);
                var instance = set.Find(Convert.ChangeType(key, descriptor.KeyProperty.PropertyType));

                var model = new Model(modelType, descriptor, instance);
                foreach (var property in model.Properties)
                {
                    var result = bindingContext.ValueProvider.GetValue(property.Name);
                    if (result != null)
                    {
                        if (!property.IsModel)
                        {
                            var converter = TypeDescriptor.GetConverter(property.MemberType);
                            var value     = property.MemberType == typeof(Boolean)
                                            ? result.AttemptedValue.Split(',')[0]
                                            : result.AttemptedValue;

                            property.Value = converter.ConvertFrom(null, CultureInfo.CurrentCulture, value);
                        }
                        else
                        {
                            var propertyTypeMapping = ModelMappingManager.FindByType(property.MemberType);
                            var propertyDescriptor  = new ModelDescriptor(propertyTypeMapping);
                            var propertyValue       = result.ConvertTo(propertyDescriptor.KeyProperty.PropertyType);
                            property.Value = context.Set(property.MemberType).Find(propertyValue);
                        }
                    }
                }
                context.Entry(model.Instance).State = EntityState.Detached;
                return(model);
            }
        }
Esempio n. 4
0
        public static ModelCollection AsCollection(this Member member, ControllerContext controllerContext)
        {
            if (member.IsModelCollection)
            {
                var model = member.UnderliningModel;

                var items = default(IQueryable);
                if (member.Value != null)
                {
                    items = ((IEnumerable)member.Value).AsQueryable();
                }
                else if (member is Parameter)
                {
                    var context = ModelAssemblies.GetContext(model.ModelType);
                    items = context.Set(model.ModelType);
                }

                var attr       = member.Attributes.OfType <QueryAttribute>().FirstOrDefault();
                var result     = QueryParser.Parse(model.ModelType, items, attr).Execute(true);
                var descriptor = new ModelDescriptor(ModelMappingManager.MappingFor(result.ElementType));
                return(new ModelCollection(model.ModelType, descriptor, result));
            }
            return(null);
        }