本文整理汇总了C#中ExtensibleModelBindingContext类的典型用法代码示例。如果您正苦于以下问题:C# ExtensibleModelBindingContext类的具体用法?C# ExtensibleModelBindingContext怎么用?C# ExtensibleModelBindingContext使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ExtensibleModelBindingContext类属于命名空间,在下文中一共展示了ExtensibleModelBindingContext类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: BindModel
public void BindModel() {
// Arrange
ControllerContext controllerContext = new ControllerContext();
ExtensibleModelBindingContext bindingContext = new ExtensibleModelBindingContext() {
ModelMetadata = new EmptyModelMetadataProvider().GetMetadataForType(null, typeof(int[])),
ModelName = "someName",
ModelBinderProviders = new ModelBinderProviderCollection(),
ValueProvider = new SimpleValueProvider() {
{ "someName[0]", "42" },
{ "someName[1]", "84" }
}
};
Mock<IExtensibleModelBinder> mockIntBinder = new Mock<IExtensibleModelBinder>();
mockIntBinder
.Expect(o => o.BindModel(controllerContext, It.IsAny<ExtensibleModelBindingContext>()))
.Returns(
delegate(ControllerContext cc, ExtensibleModelBindingContext mbc) {
mbc.Model = mbc.ValueProvider.GetValue(mbc.ModelName).ConvertTo(mbc.ModelType);
return true;
});
bindingContext.ModelBinderProviders.RegisterBinderForType(typeof(int), mockIntBinder.Object, false /* suppressPrefixCheck */);
// Act
bool retVal = new ArrayModelBinder<int>().BindModel(controllerContext, bindingContext);
// Assert
Assert.IsTrue(retVal);
int[] array = bindingContext.Model as int[];
CollectionAssert.AreEqual(new int[] { 42, 84 }, array);
}
示例2: TryBindStrongModel_BinderExists_BinderReturnsIncorrectlyTypedObject_ReturnsTrue
public void TryBindStrongModel_BinderExists_BinderReturnsIncorrectlyTypedObject_ReturnsTrue() {
// Arrange
ControllerContext controllerContext = new ControllerContext();
ExtensibleModelBindingContext bindingContext = new ExtensibleModelBindingContext() {
ModelMetadata = new EmptyModelMetadataProvider().GetMetadataForType(null, typeof(int)),
ModelName = "someName",
ModelState = new ModelStateDictionary(),
ModelBinderProviders = new ModelBinderProviderCollection(),
ValueProvider = new SimpleValueProvider()
};
Mock<IExtensibleModelBinder> mockIntBinder = new Mock<IExtensibleModelBinder>();
mockIntBinder
.Setup(o => o.BindModel(controllerContext, It.IsAny<ExtensibleModelBindingContext>()))
.Returns(
delegate(ControllerContext cc, ExtensibleModelBindingContext mbc) {
Assert.AreEqual("someName.key", mbc.ModelName);
return true;
});
bindingContext.ModelBinderProviders.RegisterBinderForType(typeof(int), mockIntBinder.Object, true /* suppressPrefixCheck */);
// Act
int model;
bool retVal = KeyValuePairModelBinderUtil.TryBindStrongModel<int>(controllerContext, bindingContext, "key", new EmptyModelMetadataProvider(), out model);
// Assert
Assert.IsTrue(retVal);
Assert.AreEqual(default(int), model);
Assert.AreEqual(1, bindingContext.ValidationNode.ChildNodes.Count, "Child validation node should have been added.");
Assert.AreEqual(0, bindingContext.ModelState.Count, "ModelState should remain unmodified.");
}
示例3: ModelBinderProvidersProperty
public void ModelBinderProvidersProperty() {
// Arrange
ExtensibleModelBindingContext bindingContext = new ExtensibleModelBindingContext();
// Act & assert
MemberHelper.TestPropertyWithDefaultInstance(bindingContext, "ModelBinderProviders", new ModelBinderProviderCollection(), ModelBinderProviders.Providers);
}
示例4: BindModel_MissingValue_ReturnsTrue
public void BindModel_MissingValue_ReturnsTrue() {
// Arrange
ControllerContext controllerContext = new ControllerContext();
ExtensibleModelBindingContext bindingContext = new ExtensibleModelBindingContext() {
ModelMetadata = new EmptyModelMetadataProvider().GetMetadataForType(null, typeof(KeyValuePair<int, string>)),
ModelName = "someName",
ModelBinderProviders = new ModelBinderProviderCollection(),
ValueProvider = new SimpleValueProvider()
};
Mock<IExtensibleModelBinder> mockIntBinder = new Mock<IExtensibleModelBinder>();
mockIntBinder
.Setup(o => o.BindModel(controllerContext, It.IsAny<ExtensibleModelBindingContext>()))
.Returns(
delegate(ControllerContext cc, ExtensibleModelBindingContext mbc) {
mbc.Model = 42;
return true;
});
bindingContext.ModelBinderProviders.RegisterBinderForType(typeof(int), mockIntBinder.Object, true /* suppressPrefixCheck */);
KeyValuePairModelBinder<int, string> binder = new KeyValuePairModelBinder<int, string>();
// Act
bool retVal = binder.BindModel(controllerContext, bindingContext);
// Assert
Assert.IsTrue(retVal, "Want to return 'True' since the key was bound correctly and we want to push validation up.");
Assert.IsNull(bindingContext.Model);
CollectionAssert.AreEquivalent(new string[] { "someName.key" },
bindingContext.ValidationNode.ChildNodes.Select(n => n.ModelStateKey).ToArray());
}
示例5: ModelNameProperty
public void ModelNameProperty() {
// Arrange
ExtensibleModelBindingContext bindingContext = new ExtensibleModelBindingContext();
// Act & assert
MemberHelper.TestStringProperty(bindingContext, "ModelName", String.Empty);
}
示例6: BindModel
public void BindModel() {
// Arrange
ControllerContext controllerContext = new ControllerContext();
ExtensibleModelBindingContext bindingContext = new ExtensibleModelBindingContext() {
ModelBinderProviders = new ModelBinderProviderCollection(),
ModelMetadata = GetMetadataForObject(new Person()),
ModelName = "someName"
};
Mock<IExtensibleModelBinder> mockDtoBinder = new Mock<IExtensibleModelBinder>();
mockDtoBinder
.Expect(o => o.BindModel(controllerContext, It.IsAny<ExtensibleModelBindingContext>()))
.Returns(
delegate(ControllerContext cc, ExtensibleModelBindingContext mbc2) {
return true; // just return the DTO unchanged
});
bindingContext.ModelBinderProviders.RegisterBinderForType(typeof(ComplexModelDto), mockDtoBinder.Object, true /* suppressPrefixCheck */);
Mock<TestableMutableObjectModelBinder> mockTestableBinder = new Mock<TestableMutableObjectModelBinder>() { CallBase = true };
mockTestableBinder.Expect(o => o.EnsureModelPublic(controllerContext, bindingContext)).Verifiable();
mockTestableBinder.Expect(o => o.GetMetadataForPropertiesPublic(controllerContext, bindingContext)).Returns(new ModelMetadata[0]).Verifiable();
TestableMutableObjectModelBinder testableBinder = mockTestableBinder.Object;
testableBinder.MetadataProvider = new DataAnnotationsModelMetadataProvider();
// Act
bool retValue = testableBinder.BindModel(controllerContext, bindingContext);
// Assert
Assert.IsTrue(retValue);
Assert.IsInstanceOfType(bindingContext.Model, typeof(Person));
Assert.IsTrue(bindingContext.ValidationNode.ValidateAllProperties);
mockTestableBinder.Verify();
}
示例7: BindModel
public void BindModel() {
// Arrange
ControllerContext controllerContext = new ControllerContext();
ExtensibleModelBindingContext bindingContext = new ExtensibleModelBindingContext() {
ModelMetadata = new EmptyModelMetadataProvider().GetMetadataForType(null, typeof(IDictionary<int, string>)),
ModelName = "someName",
ModelBinderProviders = new ModelBinderProviderCollection(),
ValueProvider = new SimpleValueProvider() {
{ "someName[0]", new KeyValuePair<int, string>(42, "forty-two") },
{ "someName[1]", new KeyValuePair<int, string>(84, "eighty-four") }
}
};
Mock<IExtensibleModelBinder> mockKvpBinder = new Mock<IExtensibleModelBinder>();
mockKvpBinder
.Expect(o => o.BindModel(controllerContext, It.IsAny<ExtensibleModelBindingContext>()))
.Returns(
delegate(ControllerContext cc, ExtensibleModelBindingContext mbc) {
mbc.Model = mbc.ValueProvider.GetValue(mbc.ModelName).ConvertTo(mbc.ModelType);
return true;
});
bindingContext.ModelBinderProviders.RegisterBinderForType(typeof(KeyValuePair<int, string>), mockKvpBinder.Object, false /* suppressPrefixCheck */);
// Act
bool retVal = new DictionaryModelBinder<int, string>().BindModel(controllerContext, bindingContext);
// Assert
Assert.IsTrue(retVal);
IDictionary<int, string> dictionary = bindingContext.Model as IDictionary<int, string>;
Assert.IsNotNull(dictionary);
Assert.AreEqual(2, dictionary.Count);
Assert.AreEqual("forty-two", dictionary[42]);
Assert.AreEqual("eighty-four", dictionary[84]);
}
示例8: BindComplexCollectionFromIndexes_InfiniteIndexes
public void BindComplexCollectionFromIndexes_InfiniteIndexes() {
// Arrange
ControllerContext controllerContext = new ControllerContext();
CultureInfo culture = CultureInfo.GetCultureInfo("fr-FR");
ExtensibleModelBindingContext bindingContext = new ExtensibleModelBindingContext() {
ModelMetadata = new EmptyModelMetadataProvider().GetMetadataForType(null, typeof(int)),
ModelName = "someName",
ModelBinderProviders = new ModelBinderProviderCollection(),
ValueProvider = new SimpleValueProvider() {
{ "someName[0]", "42" },
{ "someName[1]", "100" },
{ "someName[3]", "400" }
}
};
Mock<IExtensibleModelBinder> mockIntBinder = new Mock<IExtensibleModelBinder>();
mockIntBinder
.Expect(o => o.BindModel(controllerContext, It.IsAny<ExtensibleModelBindingContext>()))
.Returns(
delegate(ControllerContext cc, ExtensibleModelBindingContext mbc) {
mbc.Model = mbc.ValueProvider.GetValue(mbc.ModelName).ConvertTo(mbc.ModelType);
return true;
});
bindingContext.ModelBinderProviders.RegisterBinderForType(typeof(int), mockIntBinder.Object, false /* suppressPrefixCheck */);
// Act
List<int> boundCollection = CollectionModelBinder<int>.BindComplexCollectionFromIndexes(controllerContext, bindingContext, null /* indexNames */);
// Assert
CollectionAssert.AreEqual(new int[] { 42, 100 }, boundCollection, "Binding should have halted at missing element.");
CollectionAssert.AreEquivalent(new string[] { "someName[0]", "someName[1]" }, bindingContext.ValidationNode.ChildNodes.Select(o => o.ModelStateKey).ToArray());
}
示例9: ModelNameProperty
public void ModelNameProperty() {
// Arrange
ExtensibleModelBindingContext bindingContext = new ExtensibleModelBindingContext();
// Act & assert
MemberHelper.TestStringProperty(bindingContext, "ModelName", String.Empty, false /* testDefaultValue */, true /* allowNullAndEmpty */);
}
示例10: BindComplexCollectionFromIndexes_FiniteIndexes
public void BindComplexCollectionFromIndexes_FiniteIndexes()
{
// Arrange
ControllerContext controllerContext = new ControllerContext();
CultureInfo culture = CultureInfo.GetCultureInfo("fr-FR");
ExtensibleModelBindingContext bindingContext = new ExtensibleModelBindingContext
{
ModelMetadata = new EmptyModelMetadataProvider().GetMetadataForType(null, typeof(int)),
ModelName = "someName",
ModelBinderProviders = new ModelBinderProviderCollection(),
ValueProvider = new SimpleValueProvider
{
{ "someName[foo]", "42" },
{ "someName[baz]", "200" }
}
};
Mock<IExtensibleModelBinder> mockIntBinder = new Mock<IExtensibleModelBinder>();
mockIntBinder
.Setup(o => o.BindModel(controllerContext, It.IsAny<ExtensibleModelBindingContext>()))
.Returns(
delegate(ControllerContext cc, ExtensibleModelBindingContext mbc)
{
mbc.Model = mbc.ValueProvider.GetValue(mbc.ModelName).ConvertTo(mbc.ModelType);
return true;
});
bindingContext.ModelBinderProviders.RegisterBinderForType(typeof(int), mockIntBinder.Object, false /* suppressPrefixCheck */);
// Act
List<int> boundCollection = CollectionModelBinder<int>.BindComplexCollectionFromIndexes(controllerContext, bindingContext, new[] { "foo", "bar", "baz" });
// Assert
Assert.Equal(new[] { 42, 0, 200 }, boundCollection.ToArray());
Assert.Equal(new[] { "someName[foo]", "someName[baz]" }, bindingContext.ValidationNode.ChildNodes.Select(o => o.ModelStateKey).ToArray());
}
示例11: ModelStateProperty
public void ModelStateProperty() {
// Arrange
ExtensibleModelBindingContext bindingContext = new ExtensibleModelBindingContext();
ModelStateDictionary modelState = new ModelStateDictionary();
// Act & assert
MemberHelper.TestPropertyWithDefaultInstance(bindingContext, "ModelState", modelState);
}
示例12: ModelNameProperty
public void ModelNameProperty()
{
// Arrange
ExtensibleModelBindingContext bindingContext = new ExtensibleModelBindingContext();
// Act & assert
Assert.Reflection.StringProperty(bindingContext, (context) => context.ModelName, String.Empty);
}
示例13: ModelProperty
public void ModelProperty() {
// Arrange
ExtensibleModelBindingContext bindingContext = new ExtensibleModelBindingContext() {
ModelMetadata = new EmptyModelMetadataProvider().GetMetadataForType(null, typeof(int))
};
// Act & assert
MemberHelper.TestPropertyValue(bindingContext, "Model", 42);
}
示例14: ModelProperty_ThrowsIfModelMetadataDoesNotExist
public void ModelProperty_ThrowsIfModelMetadataDoesNotExist()
{
// Arrange
ExtensibleModelBindingContext bindingContext = new ExtensibleModelBindingContext();
// Act & assert
Assert.Throws<InvalidOperationException>(
delegate { bindingContext.Model = null; },
"The ModelMetadata property must be set before accessing this property.");
}
示例15: GetBinder_NoPrefixInValueProvider_ReturnsNull
public void GetBinder_NoPrefixInValueProvider_ReturnsNull() {
// Arrange
ExtensibleModelBindingContext bindingContext = new ExtensibleModelBindingContext() {
ModelMetadata = new EmptyModelMetadataProvider().GetMetadataForType(() => 42, typeof(int)),
ModelName = "foo",
ValueProvider = new SimpleValueProvider()
};
MutableObjectModelBinderProvider binderProvider = new MutableObjectModelBinderProvider();
// Act
IExtensibleModelBinder binder = binderProvider.GetBinder(null, bindingContext);
// Assert
Assert.IsNull(binder);
}