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 modelType = ModelBinders.Binders[typeof(Type)].BindModel(controllerContext, bindingContext) as Type;

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

            using (var repository = mapping.Configuration.Repository())
            {
                var keyValues = descriptor.KeyProperty.PropertyType == typeof (Guid)
                    ? Guid.Parse(key.ToString())
                    : Convert.ChangeType(key, descriptor.KeyProperty.PropertyType);
                var instance = repository.Find(keyValues);

                var model = new Model(modelType, descriptor, instance);
                foreach (var property in model.Properties)
                {
                    ValueProviderResult result;
                    if (property.IsModelCollection)
                    {
                        var type = typeof(List<>).MakeGenericType(property.UnderliningModel.ModelType);
                        var collection = Activator.CreateInstance(type);
                        var index = 0;
                        while ((result = bindingContext.ValueProvider.GetValue(string.Format("{0}[{1}]", property.Name, index))) != null)
                        {
                            var item = GetModelValue(result, property.UnderliningModel.ModelType);
                            foreach (var itemProperty in property.UnderliningModel.Properties)
                            {
                                var itemPropertyName = string.Format("{0}[{1}].{2}", property.Name, index, itemProperty.Name);
                                var itemResult = bindingContext.ValueProvider.GetValue(itemPropertyName);
                                if (itemResult != null)
                                {
                                    itemProperty.Value = !itemProperty.IsModel
                                        ? GetNonModelValue(itemResult, itemProperty.MemberType)
                                        : GetModelValue(itemResult, itemProperty.MemberType);
                                }
                            }
                            type.GetMethod("Add").Invoke(collection, new[] { item });
                            index++;
                        }
                        property.Value = collection;
                    }
                    else
                    {
                        result = bindingContext.ValueProvider.GetValue(property.Name);
                        if (result != null)
                        {
                            property.Value = !property.IsModel
                                ? GetNonModelValue(result, property.MemberType)
                                : GetModelValue(result, property.MemberType);
                        }
                    }
                }
                return model;
            }
        }
Exemple #3
0
        public Model(Type modelType, ModelDescriptor descriptor, object instance = null)
        {
            if (modelType == null)
                throw new ArgumentNullException("modelType");
            if (descriptor == null)
                throw new ArgumentNullException("descriptor");

            ModelType = modelType;
            Descriptor = descriptor;
            Instance = instance;

            properties.AddRange(descriptor.Properties.Select(p => p.AsModel(this)));
        }
        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;

            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));
        }
        public static IEnumerable<SelectListItem> GetSelectListItems(this Member member, ControllerContext controllerContext, object selectedValue)
        {
            var items = new List<SelectListItem> { new SelectListItem() };
            if(member.IsModel)
            {
                var actionName = controllerContext.RouteData.Values["action"].ToString();
                var action = (RunningObjectsAction)Enum.Parse(typeof(RunningObjectsAction), actionName);
                var result = member.Query(controllerContext).Execute();

                var mapping = ModelMappingManager.MappingFor(result.ElementType);
                var elementDescriptor = new ModelDescriptor(mapping);
                foreach (object item in result)
                {
                    var listItem = new SelectListItem();
                    var value = elementDescriptor.KeyProperty.GetValue(item);
                    listItem.Value = value != null ? value.ToString() : item.ToString();

                    if (elementDescriptor.TextProperty != null)
                    {
                        var textProperty = elementDescriptor.TextProperty.GetValue(item);
                        if (textProperty != null)
                            listItem.Text = textProperty.ToString();
                    }
                    else
                    {
                        var boxed = item;
                        listItem.Text = boxed == null ? string.Empty : boxed.ToString();
                    }

                    if (selectedValue != null)
                    {
                        var descriptor = new ModelDescriptor(ModelMappingManager.FindByType(member.MemberType));
                        var modelValue = descriptor.KeyProperty.GetValue(selectedValue);
                        if (modelValue != null)
                            listItem.Selected = listItem.Value == modelValue.ToString();
                    }

                    items.Add(listItem);
                }
            }
            return items;
        }
        public static ModelCollection ToModelCollection(this Member member)
        {
            if (member.IsModelCollection)
            {
                var model = member.UnderliningModel;

                var items = (member.Value != null)
                                ? ((IEnumerable)member.Value)
                                : ((IEnumerable)Activator.CreateInstance(typeof(List<>).MakeGenericType(member.UnderliningModel.ModelType)));

                var attr = member.Attributes.OfType<QueryAttribute>().FirstOrDefault();
                var result = QueryParser.Parse(model.ModelType, items.AsQueryable(), attr).Execute(true);

                var type = result != null ? result.ElementType : model.ModelType;
                var descriptor = new ModelDescriptor(ModelMappingManager.MappingFor(type));
                return new ModelCollection(type, descriptor, result);

            }
            return null;
        }
        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;
            }
        }
        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;
        }
 internal static object GetModelValue(ValueProviderResult result, Type memberType)
 {
     var memberMapping = ModelMappingManager.MappingFor(memberType);
     var descriptor = new ModelDescriptor(memberMapping);
     var value = result.ConvertTo(descriptor.KeyProperty.PropertyType);
     return memberMapping.Configuration.Repository().Find(value);
 }