forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCollectionModelBinder.cs
More file actions
133 lines (114 loc) · 6.18 KB
/
Copy pathCollectionModelBinder.cs
File metadata and controls
133 lines (114 loc) · 6.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.Globalization;
using System.Web.Http.Controllers;
using System.Web.Http.Internal;
using System.Web.Http.ValueProviders;
using System.Web.Http.ValueProviders.Providers;
namespace System.Web.Http.ModelBinding.Binders
{
public class CollectionModelBinder<TElement> : IModelBinder
{
// Used when the ValueProvider contains the collection to be bound as multiple elements, e.g. foo[0], foo[1].
private static List<TElement> BindComplexCollection(HttpActionContext actionContext, ModelBindingContext bindingContext)
{
string indexPropertyName = ModelBindingHelper.CreatePropertyModelName(bindingContext.ModelName, "index");
ValueProviderResult valueProviderResultIndex = bindingContext.ValueProvider.GetValue(indexPropertyName);
IEnumerable<string> indexNames = CollectionModelBinderUtil.GetIndexNamesFromValueProviderResult(valueProviderResultIndex);
return BindComplexCollectionFromIndexes(actionContext, bindingContext, indexNames);
}
internal static List<TElement> BindComplexCollectionFromIndexes(HttpActionContext actionContext, ModelBindingContext bindingContext, IEnumerable<string> indexNames)
{
bool indexNamesIsFinite;
if (indexNames != null)
{
indexNamesIsFinite = true;
}
else
{
indexNamesIsFinite = false;
indexNames = CollectionModelBinderUtil.GetZeroBasedIndexes();
}
List<TElement> boundCollection = new List<TElement>();
foreach (string indexName in indexNames)
{
string fullChildName = ModelBindingHelper.CreateIndexModelName(bindingContext.ModelName, indexName);
ModelBindingContext childBindingContext = new ModelBindingContext(bindingContext)
{
ModelMetadata = actionContext.GetMetadataProvider().GetMetadataForType(null, typeof(TElement)),
ModelName = fullChildName
};
bool didBind = false;
object boundValue = null;
if (actionContext.Bind(childBindingContext))
{
didBind = true;
boundValue = childBindingContext.Model;
// merge validation up
bindingContext.ValidationNode.ChildNodes.Add(childBindingContext.ValidationNode);
}
// infinite size collection stops on first bind failure
if (!didBind && !indexNamesIsFinite)
{
break;
}
boundCollection.Add(ModelBindingHelper.CastOrDefault<TElement>(boundValue));
}
return boundCollection;
}
public virtual bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext)
{
ModelBindingHelper.ValidateBindingContext(bindingContext);
if (!bindingContext.ValueProvider.ContainsPrefix(bindingContext.ModelName))
{
return false;
}
ValueProviderResult valueProviderResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
List<TElement> boundCollection = (valueProviderResult != null)
? BindSimpleCollection(actionContext, bindingContext, valueProviderResult.RawValue, valueProviderResult.Culture)
: BindComplexCollection(actionContext, bindingContext);
bool retVal = CreateOrReplaceCollection(actionContext, bindingContext, boundCollection);
return retVal;
}
// Used when the ValueProvider contains the collection to be bound as a single element, e.g. the raw value
// is [ "1", "2" ] and needs to be converted to an int[].
internal static List<TElement> BindSimpleCollection(HttpActionContext actionContext, ModelBindingContext bindingContext, object rawValue, CultureInfo culture)
{
if (rawValue == null)
{
return null; // nothing to do
}
List<TElement> boundCollection = new List<TElement>();
object[] rawValueArray = ModelBindingHelper.RawValueToObjectArray(rawValue);
foreach (object rawValueElement in rawValueArray)
{
ModelBindingContext innerBindingContext = new ModelBindingContext(bindingContext)
{
ModelMetadata = actionContext.GetMetadataProvider().GetMetadataForType(null, typeof(TElement)),
ModelName = bindingContext.ModelName,
ValueProvider = new CompositeValueProvider
{
new ElementalValueProvider(bindingContext.ModelName, rawValueElement, culture), // our temporary provider goes at the front of the list
bindingContext.ValueProvider
}
};
object boundValue = null;
if (actionContext.Bind(innerBindingContext))
{
boundValue = innerBindingContext.Model;
bindingContext.ValidationNode.ChildNodes.Add(innerBindingContext.ValidationNode);
}
boundCollection.Add(ModelBindingHelper.CastOrDefault<TElement>(boundValue));
}
return boundCollection;
}
// Extensibility point that allows the bound collection to be manipulated or transformed before
// being returned from the binder.
protected virtual bool CreateOrReplaceCollection(HttpActionContext actionContext, ModelBindingContext bindingContext, IList<TElement> newCollection)
{
CollectionModelBinderUtil.CreateOrReplaceCollection(bindingContext, newCollection, () => new List<TElement>());
return true;
}
}
}