public void BindModelWithPrefixAndFallback() { // Arrange ModelWithoutBindAttribute model = new ModelWithoutBindAttribute() { Foo = "FooPreValue", Bar = "BarPreValue", Baz = "BazPreValue", }; ModelBindingContext bindingContext = new ModelBindingContext() { FallbackToEmptyPrefix = true, Model = model, ModelName = "prefix", ModelType = typeof(ModelWithoutBindAttribute), ValueProvider = new ValueProviderDictionary(null) { { "foo", new ValueProviderResult("FooPostValue", "FooPostValue", null) }, { "bar", new ValueProviderResult("BarPostValue", "BarPostValue", null) } } }; DefaultModelBinder binder = new DefaultModelBinder(); // Act object updatedModel = binder.BindModel(null, bindingContext); // Assert Assert.AreSame(model, updatedModel, "Should have returned same instance of the model."); Assert.AreEqual("FooPostValue", model.Foo, "Foo property should have been updated."); Assert.AreEqual("BarPostValue", model.Bar, "Bar property should have been updated."); Assert.AreEqual("BazPreValue", model.Baz, "Baz property shouldn't have been updated since it wasn't part of the request."); }
public void BindModelWithPrefixReturnsNullIfFallbackNotSpecifiedAndValueProviderContainsNoEntries() { // Arrange ModelWithoutBindAttribute model = new ModelWithoutBindAttribute() { Foo = "FooPreValue", Bar = "BarPreValue", Baz = "BazPreValue", }; ModelBindingContext bindingContext = new ModelBindingContext() { Model = model, ModelName = "prefix", ModelType = typeof(ModelWithoutBindAttribute), ValueProvider = new ValueProviderDictionary(null) { { "foo", new ValueProviderResult("FooPostValue", "FooPostValue", null) }, { "bar", new ValueProviderResult("BarPostValue", "BarPostValue", null) } } }; DefaultModelBinder binder = new DefaultModelBinder(); // Act object updatedModel = binder.BindModel(null, bindingContext); // Assert Assert.IsNull(updatedModel); }
public void BindModelCanBindObjects() { // Arrange ControllerContext controllerContext = new Mock<ControllerContext>().Object; ModelWithoutBindAttribute model = new ModelWithoutBindAttribute() { Foo = "FooPreValue", Bar = "BarPreValue", Baz = "BazPreValue", }; ModelBindingContext bindingContext = new ModelBindingContext() { Model = model, ModelType = typeof(ModelWithoutBindAttribute), ValueProvider = new Dictionary<string, ValueProviderResult>() { { "Foo", null }, { "Bar", null } } }; Mock<IModelBinder> mockInnerBinder = new Mock<IModelBinder>(); mockInnerBinder .Expect(b => b.BindModel(It.IsAny<ControllerContext>(), It.IsAny<ModelBindingContext>())) .Returns( delegate(ControllerContext cc, ModelBindingContext bc) { Assert.AreEqual(controllerContext, cc, "ControllerContext was not forwarded correctly."); Assert.AreEqual(bindingContext.ValueProvider, bc.ValueProvider, "Value provider was not forwarded correctly."); return bc.ModelName + "PostValue"; }); DefaultModelBinder binder = new DefaultModelBinder() { Binders = new ModelBinderDictionary() { { typeof(string), mockInnerBinder.Object } } }; // Act object updatedModel = binder.BindModel(controllerContext, bindingContext); // Assert Assert.AreSame(model, updatedModel, "Should have returned same instance of the model."); Assert.AreEqual("FooPostValue", model.Foo, "Foo property should have been updated."); Assert.AreEqual("BarPostValue", model.Bar, "Bar property should have been updated."); Assert.AreEqual("BazPreValue", model.Baz, "Baz property shouldn't have been updated since it wasn't part of the request."); }