diff --git a/src/Microsoft.AspNet.Mvc.Core/BindAttribute.cs b/src/Microsoft.AspNet.Mvc.Core/BindAttribute.cs index 782da1164b..1a8556ca27 100644 --- a/src/Microsoft.AspNet.Mvc.Core/BindAttribute.cs +++ b/src/Microsoft.AspNet.Mvc.Core/BindAttribute.cs @@ -2,6 +2,7 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; +using System.Collections.Generic; using System.Linq; using System.Reflection; using Microsoft.AspNet.Mvc.Core; @@ -30,13 +31,19 @@ public class BindAttribute : Attribute, IModelNameProvider, IPropertyBindingPred /// Names of parameters to include in binding. public BindAttribute(params string[] include) { - Include = include; + var items = new List(); + foreach (var item in include) + { + items.AddRange(SplitString(item)); + } + + Include = items.ToArray(); } /// /// Creates a new instance of . /// - /// The type which implements + /// The type which implements /// . /// public BindAttribute([NotNull] Type predicateProviderType) @@ -92,7 +99,7 @@ public Func PropertyFilter { if (_predicateFromInclude == null) { - _predicateFromInclude = + _predicateFromInclude = (context, propertyName) => Include.Contains(propertyName, StringComparer.Ordinal); } @@ -136,5 +143,17 @@ private static Func CreatePredicateFromProvid return predicate(context, propertyName); }; } + + private static IEnumerable SplitString(string original) + { + if (string.IsNullOrEmpty(original)) + { + return new string[0]; + } + + var split = original.Split(',').Select(piece => piece.Trim()).Where(piece => !string.IsNullOrEmpty(piece)); + + return split; + } } } diff --git a/test/Microsoft.AspNet.Mvc.Core.Test/BindAttributeTest.cs b/test/Microsoft.AspNet.Mvc.Core.Test/BindAttributeTest.cs index d69cc49842..1fe3ea8509 100644 --- a/test/Microsoft.AspNet.Mvc.Core.Test/BindAttributeTest.cs +++ b/test/Microsoft.AspNet.Mvc.Core.Test/BindAttributeTest.cs @@ -44,10 +44,15 @@ public void Constructor_SetsThe_PropertyFilterProviderType_ForValidTypes(Type ty [InlineData("UserName", true)] [InlineData("Username", false)] [InlineData("Password", false)] + [InlineData("LastName", true)] + [InlineData("MiddleName", true)] + [InlineData(" ", false)] + [InlineData("foo", true)] + [InlineData("bar", true)] public void BindAttribute_Include(string property, bool isIncluded) { // Arrange - var bind = new BindAttribute(new string[] { "UserName", "FirstName" }); + var bind = new BindAttribute(new string[] { "UserName", "FirstName", "LastName, MiddleName, ,foo,bar " }); var context = new ModelBindingContext(); @@ -74,7 +79,7 @@ public void BindAttribute_ProviderType(string property, bool isIncluded) HttpContext = new DefaultHttpContext(), }; var services = new Mock(); - + context.OperationBindingContext.HttpContext.RequestServices = services.Object; // Act @@ -99,7 +104,7 @@ public void BindAttribute_ProviderType_Cached() }; var services = new Mock(MockBehavior.Strict); - + context.OperationBindingContext.HttpContext.RequestServices = services.Object; // Act