public void BinderShouldBindValues() { var controllerContext = new ControllerContext(); var binders = new ModelBinderDictionary { { typeof(Foo), new DefaultModelBinder() } }; var input = new FormCollection { { "fooInstance[Bar1].Value", "bar1value" }, { "fooInstance[Bar2].Value", "bar2value" } }; var foos = new[] { new Foo {Name = "Bar1", Value = "uno"}, new Foo {Name = "Bar2", Value = "dos"}, new Foo {Name = "Bar3", Value = "tres"}, }; var providers = new EmptyModelMetadataProvider(); var bindingContext = new ModelBindingContext { ModelMetadata = providers.GetMetadataForType(() => foos, foos.GetType()), ModelName = "fooInstance", ValueProvider = input.ToValueProvider() }; var binder = new KeyedListModelBinder<Foo>(binders, providers, foo => foo.Name); var result = (IList<Foo>)binder.BindModel(controllerContext, bindingContext); Assert.That(result.Count, Is.EqualTo(3)); Assert.That(result[0].Value, Is.EqualTo("bar1value")); Assert.That(result[1].Value, Is.EqualTo("bar2value")); }
private static ModelValidationNode GetValidationNode() { EmptyModelMetadataProvider provider = new EmptyModelMetadataProvider(); ModelMetadata metadata = provider.GetMetadataForType(null, typeof(object)); return new ModelValidationNode(metadata, "someKey"); }
private static ModelMetadata GetModelMetadata() { EmptyModelMetadataProvider provider = new EmptyModelMetadataProvider(); return provider.GetMetadataForType(null, typeof(object)); }
public ControllerData(Type type) { var metadataProvider = new EmptyModelMetadataProvider(); this.Properties = (from p in type.GetProperties() let attr = p.GetCustomAttributes(typeof(FromRouteAttr), inherit: true) .Cast<FromRouteAttr>() .SingleOrDefault() where attr != null select new PropertyData( property: p, attribute: attr, metadata: metadataProvider.GetMetadataForType(null, p.PropertyType) )).ToArray(); }