public void BindingListsWithIndex_ProducesSingleLengthCollection_WithNullValues()
        {
            // Arrange
            string        propertyName  = "Addresses";
            ModelMetadata modelMetadata = new EmptyModelMetadataProvider().GetMetadataForProperty(
                modelAccessor: null,
                containerType: typeof(UserWithAddress),
                propertyName: propertyName);
            ModelBindingContext bindingContext = new ModelBindingContext
            {
                ModelMetadata = modelMetadata,
                ModelName     = propertyName,
                ValueProvider = new SimpleHttpValueProvider {
                    { "Addresses.index", "10000" }
                }
            };
            HttpActionContext context = ContextUtil.CreateActionContext();
            CollectionModelBinder <UserWithAddress> binder = new CollectionModelBinder <UserWithAddress>();

            // Act
            bool result = binder.BindModel(context, bindingContext);

            // Assert
            Assert.True(result);
            List <UserWithAddress> boundModel = Assert.IsType <List <UserWithAddress> >(bindingContext.Model);
            UserWithAddress        listModel  = Assert.Single(boundModel);

            Assert.Null(listModel);
        }
        public async Task BindModel_WithIncorrectlyFormattedNestedCollectionValue()
        {
            // Arrange
            Dictionary <string, string> bodyParameters = new Dictionary <string, string>
            {
                { "Addresses", "Street Address 00" },
            };
            HttpRequestMessage request = new HttpRequestMessage(
                HttpMethod.Post,
                "http://localhost/ModelBinding/NestedCollection"
                )
            {
                Content = new FormUrlEncodedContent(bodyParameters)
            };

            // Act
            HttpResponseMessage response = await SubmitRequestAsync(request);

            // Assert
            UserWithAddress result = await ReadAsJson <UserWithAddress>(response);

            Address address = Assert.Single(result.Addresses);

            Assert.Null(address.AddressLines);
            Assert.Null(address.ZipCode);
        }
        public async Task BindModel_WithNestedCollection()
        {
            // Arrange
            Dictionary <string, string> bodyParameters = new Dictionary <string, string>
            {
                { "Addresses[0].AddressLines[0].Line", "Street Address 00" },
                { "Addresses[0].AddressLines[1].Line", "Street Address 01" },
                { "Addresses[0].ZipCode", "98052" },
                { "Addresses[1].AddressLines[0].Line", "Street Address 10" },
                { "Addresses[1].AddressLines[3].Line", "Street Address 13" },
            };
            HttpRequestMessage request = new HttpRequestMessage(
                HttpMethod.Post,
                "http://localhost/ModelBinding/NestedCollection"
                )
            {
                Content = new FormUrlEncodedContent(bodyParameters)
            };

            // Act
            HttpResponseMessage response = await SubmitRequestAsync(request);

            // Assert
            UserWithAddress result = await ReadAsJson <UserWithAddress>(response);

            Assert.Equal(2, result.Addresses.Count);
            Address address = result.Addresses[0];

            Assert.Equal(2, address.AddressLines.Count);
            Assert.Equal("Street Address 00", address.AddressLines[0].Line);
            Assert.Equal("Street Address 01", address.AddressLines[1].Line);
            Assert.Equal("98052", address.ZipCode);

            address = result.Addresses[1];
            StreetAddress streetAddress = streetAddress = Assert.Single(address.AddressLines);

            Assert.Equal("Street Address 10", streetAddress.Line);
            Assert.Null(address.ZipCode);
        }
 public UserWithAddress NestedCollectionType([FromBody] UserWithAddress user)
 {
     return(user);
 }