ASP.NET MVC - 自定义模型绑定的接口类型 [英] ASP.NET MVC - Custom Model Binder on Interface Type
问题描述
我不知道这是正常现象或没有,但似乎当绑定被分配给一个接口类型的自定义模型绑定不起作用。有没有人尝试过呢?
I'm not sure if this behavior is expected or not, but it seems that custom model binding doesn't work when the binding is assigned to an interface type. Has anyone experimented with this?
public interface ISomeModel {}
public class SomeModel : ISomeModel {}
public class MvcApplication : HttpApplication {
protected void Application_Start(object sender, EventArgs e) {
ModelBinders.Binders[typeof(ISomeModel)] = new MyCustomModelBinder();
}
}
通过上面的code,当我绑定到类型SomeModel的典范,是MyCustomModelBinder从不打;但是,如果我更改上面的code和替代 typeof运算(ISomeModel)
为 typeof运算(SomeModel)
和后如预期完全相同的形式MyCustomModelBinder被调用。这是否显得吧?
With the above code when I bind to a model of type SomeModel, MyCustomModelBinder is never hit; however, if I change the above code and substitute typeof(ISomeModel)
for typeof(SomeModel)
and post the exact same form MyCustomModelBinder is called as expected. Does that seem right?
修改
我发现自己回来了,这predicament了一年多后,我原来问过这个问题,现在我有一个有效的解决方案。谢谢马特Hidinger!
I found myself back in this predicament over a year after I originally asked this question, and now I have a solution that works. Thank you Matt Hidinger!
<一个href=\"http://www.matthidinger.com/archive/2011/08/16/An-inheritance-aware-ModelBinderProvider-in-MVC-3.aspx\">http://www.matthidinger.com/archive/2011/08/16/An-inheritance-aware-ModelBinderProvider-in-MVC-3.aspx
推荐答案
我不知道如果直接关系,但肯定有您需要考虑使用模型绑定和接口时的事情......我碰到了类似的使用默认的模型绑定的问题,但它可能不依赖于直接相关的,你是怎么做的事情...
I'm not sure if its directly related but yes there are things that you need to think about when using model binding and interfaces... I ran into similar problems with the default model binder, but it may not be directly related depending on how you are doing things...
看一看以下几点:
ASP.net MVC V2 - 调试模型绑定问题 - BUG
<一href=\"http://stackoverflow.com/questions/1676731/asp-net-mvc-v2-debugging-model-binding-issues-bug\">http://stackoverflow.com/questions/1676731/asp-net-mvc-v2-debugging-model-binding-issues-bug
这篇关于ASP.NET MVC - 自定义模型绑定的接口类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!