vdh*_*ant 21 asp.net asp.net-mvc modelbinders asp.net-mvc-2
我试图调试为什么MVC在给定的情况下没有正确绑定我有一点点困难我...
基本上,我有我的动作接收一个复杂的对象,而这个对象又有一个复杂的子对象 - Activity.Location.State(其中Activity是动作所期望的复杂对象,Location是一个复杂的子对象,State只是一个字符串) .
现在我建立了一个测试项目,据我所知,我确切地模仿了我的实际场景,在这个测试用例中绑定有效...但在我的实际项目中,绑定到Activity工作但不是位置...通过在Locaiton属性中放置断点,我可以告诉MVC从Activity中检索复杂的Location对象,但它没有设置任何属性......
我正在尝试调试这个问题,但我需要访问MVC v2预览2符号,这些符号似乎无法跟踪...我希望看到它实际上做了什么,一旦它拉出位置对象(对于一些我之所以认为它可能在内部失败但吞下这个例外).
关于我在这里可以做什么的任何想法......
干杯安东尼
更新:
好的我做了JW建议并直接引用MVC项目......
我发现了这个问题,并且我忽略了一个非常小的差异......正如我的结果我发现当涉及到模型绑定时,MVC当前不支持多层INTERFACE继承...请参阅以下内容......
//MODEL
public class Location : ILocation
{
...
}
public interface ILocation : ILocationCore
{
...
}
public interface ILocationCore //In my sample I didn't have this second level interface
{
...
//MVC doesn't find any of these properties
...
}
public class Activity : IActivity
{
...
}
public interface IActivity : IActivityCore
{
ILocation Location { get; set; } //MVC finds this and reads its meta type as an ILocation
//Also the implementation of this Location within Activity will always return a instance - our IoC takes care of that, so MVC should never have to create the instance
}
public interface IActivityCore
{
...
}
//CONTROLLER
public ActionResult Create(Activity activity)
{
}
Run Code Online (Sandbox Code Playgroud)
因此我发现MVC找到了Location并将其元类型作为ILocation读取,但是当在DefaultModelBinder中运行GetModelProperties时,会发生以下情况 -
protected virtual PropertyDescriptorCollection GetModelProperties(ControllerContext controllerContext, ModelBindingContext bindingContext) {
return GetTypeDescriptor(controllerContext, bindingContext).GetProperties();
//This return no properties
}
protected virtual ICustomTypeDescriptor GetTypeDescriptor(ControllerContext controllerContext, ModelBindingContext bindingContext) {
return new AssociatedMetadataTypeTypeDescriptionProvider(bindingContext.ModelType).GetTypeDescriptor(bindingContext.ModelType);
//bindingContext.ModelType - is ILocation
}
Run Code Online (Sandbox Code Playgroud)
因此,我在此假设TypeDescriptionProvider不支持这种继承方式,我很惊讶.另外看看v1源代码,看起来这是v2引入的 - 但是v1可能无法支持我想要做的事情.
我不会说这真的是一个错误,但我尝试用具体的类替换我的接口,它工作正常.因此,这种行为并不是我所期望的,而且有点不一致.
有什么想法吗???我原本以为这种继承不是很标准,但经常发生,足以满足.谢谢回复.
干杯
Haa*_*ked 45
由于接口继承的工作原理,这种行为是设计原因.接口不定义实现,因此ILocation不"继承"ILocationSource的属性.相反,ILocation仅定义具体实现必须实现的内容.
有关完整详细信息(包括定义此行为的CLI(公共语言基础结构)规范部分),请查看:http://haacked.com/archive/2009/11/10/interface-inheritance-esoterica.aspx
| 归档时间: |
|
| 查看次数: |
4764 次 |
| 最近记录: |