public async Task BindModel_ComplexCollection_Succeeds(bool isReadOnly) { // Arrange var valueProvider = new SimpleValueProvider { { "someName.index", new[] { "foo", "bar", "baz" } }, { "someName[foo]", "42" }, { "someName[bar]", "100" }, { "someName[baz]", "200" } }; var bindingContext = GetModelBindingContext(valueProvider, isReadOnly); var modelState = bindingContext.ModelState; var binder = new CollectionModelBinder <int>(); // Act var result = await binder.BindModelResultAsync(bindingContext); // Assert Assert.NotEqual(default(ModelBindingResult), result); Assert.True(result.IsModelSet); var list = Assert.IsAssignableFrom <IList <int> >(result.Model); Assert.Equal(new[] { 42, 100, 200 }, list.ToArray()); Assert.True(modelState.IsValid); }
public async Task CollectionModelBinder_DoesNotCreateEmptyCollection_IfModelNonNull() { // Arrange var binder = new CollectionModelBinder <string>(); var context = CreateContext(); context.IsTopLevelObject = true; var list = new List <string>(); context.Model = list; // Lack of prefix and non-empty model name both ignored. context.ModelName = "modelName"; var metadataProvider = context.OperationBindingContext.MetadataProvider; context.ModelMetadata = metadataProvider.GetMetadataForType(typeof(List <string>)); context.ValueProvider = new TestValueProvider(new Dictionary <string, object>()); // Act var result = await binder.BindModelResultAsync(context); // Assert Assert.NotEqual(default(ModelBindingResult), result); Assert.Same(list, result.Model); Assert.Empty(list); Assert.Equal("modelName", result.Key); Assert.True(result.IsModelSet); }
public async Task CollectionModelBinder_DoesNotCreateCollection_IfNotIsTopLevelObject(string prefix) { // Arrange var binder = new CollectionModelBinder <string>(); var context = CreateContext(); context.ModelName = ModelNames.CreatePropertyModelName(prefix, "ListProperty"); var metadataProvider = context.OperationBindingContext.MetadataProvider; context.ModelMetadata = metadataProvider.GetMetadataForProperty( typeof(ModelWithListProperty), nameof(ModelWithListProperty.ListProperty)); context.ValueProvider = new TestValueProvider(new Dictionary <string, object>()); // Act var result = await binder.BindModelResultAsync(context); // Assert Assert.Equal(default(ModelBindingResult), result); }
public async Task BindModelAsync_SimpleCollectionWithNullValue_Succeeds() { // Arrange var binder = new CollectionModelBinder <int>(); var valueProvider = new SimpleValueProvider { { "someName", null }, }; var bindingContext = GetModelBindingContext(valueProvider, isReadOnly: false); var modelState = bindingContext.ModelState; // Act var result = await binder.BindModelResultAsync(bindingContext); // Assert Assert.NotEqual(default(ModelBindingResult), result); Assert.True(result.IsModelSet); Assert.NotNull(result.Model); var model = Assert.IsType <List <int> >(result.Model); Assert.Empty(model); }
public async Task BindModel_SimpleCollection_BindingContextModelNonNull_Succeeds(bool isReadOnly) { // Arrange var valueProvider = new SimpleValueProvider { { "someName", new[] { "42", "100", "200" } } }; var bindingContext = GetModelBindingContext(valueProvider, isReadOnly); var modelState = bindingContext.ModelState; var list = new List <int>(); bindingContext.Model = list; var binder = new CollectionModelBinder <int>(); // Act var result = await binder.BindModelResultAsync(bindingContext); // Assert Assert.NotEqual(default(ModelBindingResult), result); Assert.True(result.IsModelSet); Assert.Same(list, result.Model); Assert.Equal(new[] { 42, 100, 200 }, list.ToArray()); }