10using System.Collections.Generic;
12using Microsoft.VisualStudio.TestTools.UnitTesting;
13using System.Collections.Specialized;
15using System.Globalization;
32 var values =
new NameValueCollection();
33 values.Add(
"name",
"Gimli");
34 values.Add(
"dob",
"1/1/2000");
36 var bindingContext =
new ModelBindingContext
38 ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(() => model, typeof(
SingleRoot)),
39 ValueProvider =
new NameValueCollectionValueProvider(values, CultureInfo.CurrentCulture)
42 object retModel = binder.BindModel(
new ControllerContext(), bindingContext);
44 Assert.AreSame(model, retModel,
"Return should be the same as original");
45 Assert.AreEqual(
"Gimli", model.Name);
46 Assert.AreEqual(
new SmartDate(
new DateTime(2000, 1, 1)), model.DOB);
53 var values =
new NameValueCollection();
54 values.Add(
"pfx.name",
"Gimli");
56 var bindingContext =
new ModelBindingContext
59 ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(() => model, typeof(
SingleRoot)),
60 ValueProvider =
new NameValueCollectionValueProvider(values, CultureInfo.CurrentCulture)
63 object retModel = binder.BindModel(
new ControllerContext(), bindingContext);
65 Assert.AreSame(model, retModel,
"Return should be the same as original");
66 Assert.AreEqual(
"Gimli", model.Name);
73 var values =
new NameValueCollection();
74 values.Add(
"Max5Chars",
"More Than 5 Characters");
75 values.Add(
"Between2And10",
"100");
76 values.Add(
"AlwaysInvalid",
"bla..");
78 var bindingContext =
new ModelBindingContext
80 ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(() => model, typeof(
RootWithValidation)),
81 ValueProvider =
new NameValueCollectionValueProvider(values, CultureInfo.CurrentCulture)
84 binder.BindModel(
new ControllerContext(), bindingContext);
86 Assert.IsFalse(model.IsValid,
"Object should be invalid");
87 Assert.IsFalse(bindingContext.ModelState.IsValid,
"Model State should be invalid");
88 Assert.IsFalse(bindingContext.ModelState.IsValidField(
"Max5Chars"),
"Max5Chars property should be invalid");
89 Assert.AreEqual(1, bindingContext.ModelState[
"Max5Chars"].Errors.Count);
90 Assert.IsFalse(bindingContext.ModelState.IsValidField(
"Between2And10"),
"Between2And10 property should be invalid");
91 Assert.AreEqual(1, bindingContext.ModelState[
"Between2And10"].Errors.Count);
92 Assert.IsFalse(bindingContext.ModelState.IsValidField(
"AlwaysInvalid"),
"AlwaysInvalid property should be invalid");
93 Assert.AreEqual(1, bindingContext.ModelState[
"AlwaysInvalid"].Errors.Count);
100 var values =
new NameValueCollection();
101 values.Add(
"pfx.Max5Chars",
"More Than 5 Characters");
102 values.Add(
"pfx.Between2And10",
"100");
103 values.Add(
"pfx.AlwaysInvalid",
"bla..");
105 var bindingContext =
new ModelBindingContext
108 ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(() => model, typeof(
RootWithValidation)),
109 ValueProvider =
new NameValueCollectionValueProvider(values, CultureInfo.CurrentCulture)
112 binder.BindModel(
new ControllerContext(), bindingContext);
114 Assert.IsFalse(model.IsValid,
"Object should be invalid");
115 Assert.IsFalse(bindingContext.ModelState.IsValid,
"Model State should be invalid");
116 Assert.IsFalse(bindingContext.ModelState.IsValidField(
"pfx.Max5Chars"),
"Max5Chars property should be invalid");
117 Assert.AreEqual(1, bindingContext.ModelState[
"pfx.Max5Chars"].Errors.Count);
118 Assert.IsFalse(bindingContext.ModelState.IsValidField(
"pfx.Between2And10"),
"Between2And10 property should be invalid");
119 Assert.AreEqual(1, bindingContext.ModelState[
"pfx.Between2And10"].Errors.Count);
120 Assert.IsFalse(bindingContext.ModelState.IsValidField(
"pfx.AlwaysInvalid"),
"AlwaysInvalid property should be invalid");
121 Assert.AreEqual(1, bindingContext.ModelState[
"pfx.AlwaysInvalid"].Errors.Count);
128 var values =
new NameValueCollection();
129 values.Add(
"[0].AnyString",
"Any-0");
130 values.Add(
"[0].Max5Chars",
"Max5-0");
131 values.Add(
"[1].AnyString",
"Any-1");
132 values.Add(
"[1].Max5Chars",
"Max5-1");
133 values.Add(
"[2].AnyString",
"Any-2");
134 values.Add(
"[2].Max5Chars",
"Max5-2");
136 var bindingContext =
new ModelBindingContext
138 ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(() => model, typeof(
RootList)),
139 ValueProvider =
new NameValueCollectionValueProvider(values, CultureInfo.CurrentCulture)
142 object retModel = binder.BindModel(
new ControllerContext(), bindingContext);
144 Assert.AreSame(model, retModel,
"Return should be the same as original");
145 Assert.AreEqual(
"Any-0", model[0].AnyString);
146 Assert.AreEqual(
"Max5-0", model[0].Max5Chars);
147 Assert.AreEqual(
"Any-1", model[1].AnyString);
148 Assert.AreEqual(
"Max5-1", model[1].Max5Chars);
149 Assert.AreEqual(
"Any-2", model[2].AnyString);
150 Assert.AreEqual(
"Max5-2", model[2].Max5Chars);
157 var values =
new NameValueCollection();
158 values.Add(
"pfx[0].AnyString",
"Any-0");
159 values.Add(
"pfx[0].Max5Chars",
"Max5-0");
160 values.Add(
"pfx[1].AnyString",
"Any-1");
161 values.Add(
"pfx[1].Max5Chars",
"Max5-1");
163 var bindingContext =
new ModelBindingContext
166 ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(() => model, typeof(
RootList)),
167 ValueProvider =
new NameValueCollectionValueProvider(values, CultureInfo.CurrentCulture)
170 object retModel = binder.BindModel(
new ControllerContext(), bindingContext);
172 Assert.AreSame(model, retModel,
"Return should be the same as original");
173 Assert.AreEqual(
"Any-0", model[0].AnyString);
174 Assert.AreEqual(
"Max5-0", model[0].Max5Chars);
175 Assert.AreEqual(
"Any-1", model[1].AnyString);
176 Assert.AreEqual(
"Max5-1", model[1].Max5Chars);
183 var values =
new NameValueCollection();
184 values.Add(
"[0].AnyString",
"Any-0");
185 values.Add(
"[0].Max5Chars",
"More Than 5 Characters");
187 var bindingContext =
new ModelBindingContext
189 ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(() => model, typeof(
RootList)),
190 ValueProvider =
new NameValueCollectionValueProvider(values, CultureInfo.CurrentCulture)
193 binder.BindModel(
new ControllerContext(), bindingContext);
195 Assert.IsFalse(model.IsValid,
"Object should be invalid");
196 Assert.IsFalse(bindingContext.ModelState.IsValid,
"Model State should be invalid");
197 Assert.IsFalse(bindingContext.ModelState.IsValidField(
"[0].Max5Chars"),
"Max5Chars property should be invalid");
198 Assert.AreEqual(1, bindingContext.ModelState[
"[0].Max5Chars"].Errors.Count);
205 var values =
new NameValueCollection();
206 values.Add(
"id",
"20");
207 values.Add(
"name",
"Gimli");
208 values.Add(
"children[0].AnyString",
"Any-0");
209 values.Add(
"children[0].Max5Chars",
"Max5-0");
210 values.Add(
"children[1].AnyString",
"Any-1");
211 values.Add(
"children[1].Max5Chars",
"Max5-1");
213 var bindingContext =
new ModelBindingContext
215 ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(() => model, typeof(
RootWithChildren)),
216 ValueProvider =
new NameValueCollectionValueProvider(values, CultureInfo.CurrentCulture)
219 object retModel = binder.BindModel(
new ControllerContext(), bindingContext);
221 Assert.AreSame(model, retModel,
"Return should be the same as original");
222 Assert.AreEqual(
"Gimli", model.Name);
223 Assert.AreEqual(
"Any-0", model.Children[0].AnyString);
224 Assert.AreEqual(
"Max5-0", model.Children[0].Max5Chars);
225 Assert.AreEqual(
"Any-1", model.Children[1].AnyString);
226 Assert.AreEqual(
"Max5-1", model.Children[1].Max5Chars);
233 var values =
new NameValueCollection();
234 values.Add(
"pfx.id",
"20");
235 values.Add(
"pfx.name",
"Gimli");
236 values.Add(
"pfx.children[0].AnyString",
"Any-0");
237 values.Add(
"pfx.children[0].Max5Chars",
"Max5-0");
238 values.Add(
"pfx.children[1].AnyString",
"Any-1");
239 values.Add(
"pfx.children[1].Max5Chars",
"Max5-1");
241 var bindingContext =
new ModelBindingContext
244 ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(() => model, typeof(
RootWithChildren)),
245 ValueProvider =
new NameValueCollectionValueProvider(values, CultureInfo.CurrentCulture)
248 object retModel = binder.BindModel(
new ControllerContext(), bindingContext);
250 Assert.AreSame(model, retModel,
"Return should be the same as original");
251 Assert.AreEqual(
"Gimli", model.Name);
252 Assert.AreEqual(
"Any-0", model.Children[0].AnyString);
253 Assert.AreEqual(
"Max5-0", model.Children[0].Max5Chars);
254 Assert.AreEqual(
"Any-1", model.Children[1].AnyString);
255 Assert.AreEqual(
"Max5-1", model.Children[1].Max5Chars);
Model binder for use with CSLA .NET editable business objects.
static void Initialize(TestContext testContext)
void CanBind_To_RootList()
void Bind_InvalidValues_WithPrefix_ModelState_Returns_Errors()
void CanBind_WithPrefix_To_RootWithChildren()
void CanBind_To_SingleRoot()
void CanBind_To_RootWithChildren()
void CanBind_WithPrefix_To_SingleRoot()
void CanBind_WithPrefix_To_RootList()
void Bind_RootList_WithInvalidValues_ModelState_ReturnsErrors()
void Bind_InvalidValues_ModelState_Returns_Errors()
static RootList Get(int itemsCount)
static RootWithChildren Get(int childCount)
static RootWithValidation NewRootWithValidation()
static SingleRoot GetSingleRoot(int id)
Provides a date data type that understands the concept of an empty date value.