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); }
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; 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)); }
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 static IEnumerable<SelectListItem> GetSelectListItems(this Member member, IQueryable source, object selectedValue) { var items = new List<SelectListItem> {new SelectListItem()}; if (member.IsModel) { var mapping = ModelMappingManager.MappingFor(source.ElementType); var elementDescriptor = new ModelDescriptor(mapping); foreach (object item in source) { 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.MappingFor(member.MemberType)); var modelValue = descriptor.KeyProperty.GetValue(selectedValue); if (modelValue != null) listItem.Selected = listItem.Value == modelValue.ToString(); } items.Add(listItem); } } return items; }
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) { if (property.IsModelCollection) { property.Value = CreateCollection(bindingContext, property); } else { var result = bindingContext.ValueProvider.GetValue(property.Name); if (result != null) { property.Value = !property.IsModel ? GetNonModelValue(result, property.MemberType) : GetModelValue(result, property.MemberType); } } } return model; } }
private static object CreateItem(ModelBindingContext bindingContext, Property property, ValueProviderResult result, int index) { var item = GetModelValue(result, property.UnderliningModel.ModelType) ?? Activator.CreateInstance(property.UnderliningModel.ModelType); var propertyDescriptor = new ModelDescriptor(ModelMappingManager.MappingFor(property.UnderliningModel.ModelType)); var propertyModel = new Model(property.UnderliningModel.ModelType, propertyDescriptor, item); foreach (var itemProperty in propertyModel.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); } } return item; }