public object Bind(BindingContext context) { Precondition.Require(context, () => Error.ArgumentNull("context")); ValueProviderResult result = context.ValueProvider.GetValue(context.ModelName); if (result == null) return null; List<HttpPostedFileBase> files = result .GetValue<IEnumerable<HttpPostedFileBase>>().ToList(); Type type = context.ModelType; if (type == typeof(IEnumerable<HttpPostedFileBase>) || type == typeof(HttpPostedFileBase[])) return files.ToArray(); if (type == typeof(ICollection<HttpPostedFileBase>) || type == typeof(Collection<HttpPostedFileBase>)) return new Collection<HttpPostedFileBase>(files); if (type == typeof(IList<HttpPostedFileBase>) || type == typeof(List<HttpPostedFileBase>)) return files; ConstructorInfo constructor = type.GetConstructor(new Type[] { typeof(IEnumerable<HttpPostedFileBase>) }); if (constructor == null) throw Error.UnsupportedModelType(type); return constructor.CreateInvoker().Invoke(files); }
protected virtual ICustomTypeDescriptor GetTypeDescriptor(BindingContext context) { AssociatedMetadataTypeTypeDescriptionProvider provider = new AssociatedMetadataTypeTypeDescriptionProvider(context.ModelType); return provider.GetTypeDescriptor(context.ModelType, context.Model); }
private BindingContext CreateComplexModelBindingContext(BindingContext context, object result) { BindingContext inner = new BindingContext(context, context.ModelType, context.ModelName, context.ValueProvider, context.ModelState) { Model = result }; return inner; }
public BindingContext(BindingContext parent) : base(parent) { _modelType = parent._modelType; _modelName = parent._modelName; _valueProvider = parent._valueProvider; _modelState = parent._modelState; _model = parent._model; }
public object Bind(BindingContext context) { Precondition.Require(context, () => Error.ArgumentNull("context")); ValueProviderResult result = context.ValueProvider.GetValue(context.ModelName); if (result == null) return null; return result.GetValue<HttpPostedFileBase>(); }
protected virtual void OnModelUpdated(BindingContext context) { IEnumerable<ModelValidator> validators = ValidatorProvider.GetValidators(context.ModelType); ModelValidationContext inner = new ModelValidationContext(context.ModelName, context.Model); foreach (ModelValidator validator in validators) { foreach (ValidationError error in validator.Validate(inner)) context.ModelState.Add(error.Key, error); } }
protected virtual IEnumerable<PropertyDescriptor> GetModelProperties(BindingContext context) { return GetTypeDescriptor(context).GetProperties().Cast<PropertyDescriptor>() .Where(p => AllowPropertyUpdate(p)).OrderBy(p => { PropertyBindingOrderAttribute attribute = p.Attributes .OfType<PropertyBindingOrderAttribute>() .FirstOrDefault(); return (attribute == null) ? int.MaxValue : attribute.Order; }); }
public object Bind(BindingContext context) { // Заполняем FormCollection теми данными, // что были использованы как источник в // атрибуте BindAttribute. Precondition.Require(context, () => Error.ArgumentNull("context")); IValueSet source = BindDataSource(context.ValueProvider); return new FormCollection(source); }
protected override object ExecuteBind(BindingContext context) { ValueProviderResult value; if (!context.TryGetValue(out value)) return null; using (TextReader reader = new StringReader( Convert.ToString(value.Value, CultureInfo.InvariantCulture))) { XmlSerializer serializer = new XmlSerializer(context.ModelType); return context.Model = serializer.Deserialize(reader); } }
protected override object ExecuteBind(BindingContext context) { ValueProviderResult value; if (!context.TryGetValue(out value)) return null; string stringValue = value.GetValue<string>(); if (String.IsNullOrEmpty(stringValue)) return null; ModelStateSerializer serializer = new ModelStateSerializer(); return serializer.Deserialize(stringValue, _mode); }
public object Bind(BindingContext context) { IValueProvider provider = new DictionaryValueProvider(context.Parameters); // Rewrite the provided context to ensure all the parameters // provided by user is set correctly. BindingContext inner = new BindingContext(context, context.ModelType, context.ModelName, provider, context.ModelState); ValueProviderResult value; if (inner.TryGetValue(out value)) return value.Value; return null; }
private object BindObject(BindingContext context) { ValueProviderResult value; if (context.TryGetValue(out value)) return value.Value; return null; }
protected virtual bool VerifyValueUsability(BindingContext context, string elementKey, Type elementType, object value) { if (value == null && !elementType.IsNullable() && context.ModelState.IsValid(elementKey)) { string message = GetValueRequiredResource(context); context.ModelState.Add(elementKey, new ValidationError(elementKey, value, message, null)); return false; } return context.ModelState.IsValid(elementKey); }
protected virtual bool OnModelUpdating(BindingContext context) { return true; }
public object Bind(BindingContext context) { Precondition.Require(context, () => Error.ArgumentNull("context")); if (!HasBindingData(context)) { if (context.FallbackToEmptyPrefix) context.ModelName = String.Empty; else return null; } object result; if (TryBindExactValue(context, out result)) return result; return ExecuteBind(context); }
protected virtual bool TryBindExactValue(BindingContext context, out object value) { value = BindObject(context); // if the model type is System.Object, // we can not bind it using the default strategy, // so here we return the only value we have. if (context.ModelType == typeof(object)) return true; if (value == null) return false; Type valueType = value.GetType(); if (context.ModelType.IsAssignableFrom(valueType)) return true; return false; }
protected virtual void OnPropertyUpdated(BindingContext context, PropertyDescriptor property, object value) { }
protected override object ExecuteBind(BindingContext context) { object result = CreateModelInstance(context); if (result == null) return null; BindingContext inner = CreateComplexModelBindingContext(context, result); if (OnModelUpdating(inner)) { BindProperties(inner); OnModelUpdated(inner); } return result; }
protected virtual bool OnPropertyUpdating(BindingContext context, PropertyDescriptor property, object value) { string propertyKey = CreateSubMemberName(context.ModelName, property.Name); return VerifyValueUsability(context, propertyKey, property.PropertyType, value); }
protected void SetProperty(BindingContext context, PropertyDescriptor property, object value) { string propertyKey = CreateSubMemberName(context.ModelName, property.Name); if (property.IsReadOnly) return; try { property.SetValue(context.Model, value); } catch (Exception ex) { context.ModelState.Add(propertyKey, new ValidationError(ex.Message, value, ex)); } }
protected virtual object CreateModelInstance(BindingContext context) { object model = context.Model; if (model == null) { if (context.ModelType.GetConstructor(Type.EmptyTypes) == null) { context.ModelState.Add(context.ModelName, Error.MissingParameterlessConstructor(context.ModelType)); return null; } else { model = Activator.CreateInstance(context.ModelType); } } return model; }
protected virtual object GetPropertyValue(BindingContext context, PropertyDescriptor property) { IModelBinder binder = GetPropertyBinder(property); return binder.Bind(context); }
protected virtual object GetParameterValue(ControllerContext context, ParameterDescriptor parameter) { Precondition.Require(context, () => Error.ArgumentNull("context")); Precondition.Require(parameter, () => Error.ArgumentNull("parameter")); IModelBinder binder = GetBinder(parameter); BindingContext bc = new BindingContext(context, parameter.Type, parameter.Binding.Name, GetValueProvider(context, parameter.Binding.Source), GetController().ModelState); bc.FallbackToEmptyPrefix = (String.Equals(parameter.Binding.Name, parameter.Name)); return binder.Bind(bc) ?? parameter.Binding.DefaultValue; }
private static bool HasBindingData(BindingContext context) { return(context.Contains(context.ModelName)); }
protected void BindProperties(BindingContext context) { foreach (PropertyDescriptor property in GetModelProperties(context)) BindProperty(context, property); }
private static bool HasBindingData(BindingContext context) { return context.Contains(context.ModelName); }
protected abstract object ExecuteBind(BindingContext context);
protected void BindProperty(BindingContext context, PropertyDescriptor property) { string propertyKey = CreateSubMemberName(context.ModelName, property.Name); // В случае с типом значения нужно задать значение по умолчанию, // иначе частичная инициализация объекта не удастся. object value = property.PropertyType.CreateInstance(); if (context.Contains(propertyKey)) { BindingContext inner = new BindingContext(context, property.PropertyType, propertyKey, context.ValueProvider, context.ModelState) { Model = property.GetValue(context.Model) }; value = GetPropertyValue(inner, property); } if (OnPropertyUpdating(context, property, value)) { SetProperty(context, property, value); OnPropertyUpdated(context, property, value); } }