public void IListBind() { string data = @" cust.years = 2006 cust.years = 2007 cust.years = 2008 cust.years = 2009 " ; NameValueCollection args = TestUtils.ParseNameValueString(data); object instance = binder.BindObject(typeof(Customer2), "cust", builder.BuildSourceNode(args)); Assert.IsNotNull(instance); var cust = instance as Customer2; Assert.IsNotNull(cust); Assert.IsNotNull(cust.Years); Assert.AreEqual(4, cust.Years.Count); Assert.AreEqual(2006, cust.Years[0]); Assert.AreEqual(2007, cust.Years[1]); Assert.AreEqual(2008, cust.Years[2]); Assert.AreEqual(2009, cust.Years[3]); }
public void ValidateInheritedNestedNonEmpty() { var args = new NameValueCollection { { "customer.Email", "*****@*****.**" }, { "customer.ConfirmEmail", "*****@*****.**" }, { "customer.Address.Street", string.Empty } }; object instance = binder.BindObject(typeof(Customer), "customer", builder.BuildSourceNode(args)); Assert.IsNotNull(instance); Assert.AreEqual(1, binder.ErrorList.Count); }
/// <summary> /// Binds the object of the specified type using the given prefix. /// but only using the entries from the collection specified on the <paramref name="from"/> /// </summary> /// <param name="from">From.</param> /// <param name="targetType">Type of the target.</param> /// <param name="prefix">The prefix.</param> /// <param name="excludedProperties">The excluded properties, comma separated list.</param> /// <param name="allowedProperties">The allowed properties, comma separated list.</param> /// <returns></returns> protected object BindObject(ParamStore from, Type targetType, String prefix, String excludedProperties, String allowedProperties) { var node = Request.ObtainParamsNode(from); var instance = binder.BindObject(targetType, prefix, excludedProperties, allowedProperties, node); boundInstances[instance] = binder.ErrorList; PopulateValidatorErrorSummary(instance, binder.GetValidationSummary(instance)); return(instance); }
/// <summary> /// Binds the model object using a castle IDataBinder /// </summary> /// <param name="controllerContext"></param> /// <param name="bindingContext">The current binding context</param> /// <returns>A ModelBinderResult containing the bound object</returns> public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { IDataBinder binder = LocateBinder(controllerContext); string modelName = Prefix ?? bindingContext.ModelName; var tree = new TreeBuilder().BuildSourceNode(GetStore(controllerContext)); if (tree.GetChildNode(modelName) == null) { return(null); } object instance = binder.BindObject(bindingContext.ModelType, modelName, Exclude, null, tree); return(instance); }
public void TypeConverterBinding() { var args = new NameValueCollection(); args.Add("Comp.Type1", "validvalue"); var instance = (Comp)binder.BindObject(typeof(Comp), "Comp", builder.BuildSourceNode(args)); Assert.IsNotNull(instance); Assert.IsNotNull(instance.Type1); Assert.IsNull(instance.Type2); args = new NameValueCollection(); args.Add("Comp.Nonsense", "validvalue"); binder = new DataBinder(); instance = (Comp)binder.BindObject(typeof(Comp), "Comp", builder.BuildSourceNode(args)); Assert.IsNotNull(instance); Assert.IsNull(instance.Type1); Assert.IsNull(instance.Type2); }
public void DateTimeBind() { var args = new NameValueCollection(); args.Add("person.DOBday", 1.ToString()); args.Add("person.DOBmonth", 12.ToString()); args.Add("person.DOByear", 2005.ToString()); object instance = binder.BindObject(typeof(Person), "person", builder.BuildSourceNode(args)); Assert.IsNotNull(instance); var p = (Person)instance; Assert.AreEqual(new DateTime(2005, 12, 1), p.DOB); args.Clear(); args.Add("person.DOBday", 2.ToString()); args.Add("person.DOBmonth", 1.ToString()); args.Add("person.DOByear", 2005.ToString()); instance = binder.BindObject(typeof(Person), "person", builder.BuildSourceNode(args)); Assert.IsNotNull(instance); p = (Person)instance; Assert.AreEqual(new DateTime(2005, 1, 2), p.DOB); }
public void TypeConverterBinding() { var args = new NameValueCollection(); args.Add("Comp.Type1", "validvalue"); var instance = (Comp) binder.BindObject(typeof (Comp), "Comp", builder.BuildSourceNode(args)); Assert.IsNotNull(instance); Assert.IsNotNull(instance.Type1); Assert.IsNull(instance.Type2); args = new NameValueCollection(); args.Add("Comp.Nonsense", "validvalue"); binder = new DataBinder(); instance = (Comp) binder.BindObject(typeof (Comp), "Comp", builder.BuildSourceNode(args)); Assert.IsNotNull(instance); Assert.IsNull(instance.Type1); Assert.IsNull(instance.Type2); }
public void CanHandleProtoTypeSimpleArray() { string data = @"abc[]=Foo,Bar"; NameValueCollection args = TestUtils.ParseNameValueString(data); object instance = binder.BindObject(typeof(string[]), "abc", builder.BuildSourceNode(args)); Assert.IsNotNull(instance); var sc = instance as string[]; Assert.IsNotNull(sc); Assert.AreEqual(2, sc.Length); Assert.AreEqual("Foo", sc[0]); Assert.AreEqual("Bar", sc[1]); }