解决方法
我这样做:
var formElements = new NameValueCollection() { {"FirstName","Bubba"},{"MiddleName",""},{"LastName","Gump"} }; var fakeController = GetControllerContext(formElements); var valueProvider = new Mock<IValueProvider>(); var bindingContext = new ModelBindingContext(fakeController,valueProvider.Object,typeof(Guid),null,null); private static ControllerContext GetControllerContext(NameValueCollection form) { Mock<HttpRequestBase> mockRequest = new Mock<HttpRequestBase>(); mockRequest.Expect(r => r.Form).Returns(form); Mock<HttpContextBase> mockHttpContext = new Mock<HttpContextBase>(); mockHttpContext.Expect(c => c.Request).Returns(mockRequest.Object); return new ControllerContext(mockHttpContext.Object,new RouteData(),new Mock<ControllerBase>().Object); }
然后我刚刚将bindingContext变量传递给实现IModelBinder接口的对象的BindModel方法。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。