ASP.NET MVC V2-调试模型绑定问题 - 错误?
-
16-09-2019 - |
题
我在试图调试为什么MVC在给定的情况下没有正确绑定的问题有些困难,我有...
基本上,我的操作接收了一个复杂的对象,该对象又具有复杂的子对象 - activity.location.state(其中活动是动作期望的复杂对象,位置是一个复杂的子对象,状态只是字符串)) 。
现在,我设置了一个测试项目,据我所知,该项目准确地模仿了我实际情况,在这种测试案例中,绑定起来...但是在我的实际项目中,与活动的绑定起来不行,但对位置不行...通过将断点放在Locaiton属性中,我可以说MVC正在从活动中检索复杂的位置对象,但没有设置任何属性...
我正在尝试调试这个问题,但是我需要访问MVC V2预览2符号,我似乎无法追踪...我想看看它一旦拉出位置对象,它实际上在做什么(对于某些我认为它可能在内部失败,但吞咽了例外)。
关于我在这里可以做什么的任何想法...
欢呼安东尼
更新:
好的,我做了JW的建议,直接引用了MVC项目...
我发现了这个问题,我忽略了一个很小的差异...因为我发现MVC当前在模型绑定方面不支持多个接口的继承...请参阅以下...
//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)
{
}
因此,我发现MVC找到位置并将其元类型读取为ILocation,但是当GetModelProperties在DefaultModelBinder内运行时,发生以下情况 -
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
}
因此,在这一点上,我假设键入法语不支持这种继承风格,这让我感到非常惊讶。同样,查看V1源,看起来像是通过V2引入的 - 但是V1可能无法支持我要做的事情。
我不会说这确实是一个错误,但是我尝试用具体课程代替界面,而且效果很好。因此,这种行为并不是我所期望的,有点不一致。
有什么想法吗???我本来以为这种继承不是相当标准的,但经常发生足够的时间来满足。谢谢回复。
干杯
解决方案
事实证明,由于接口继承的工作方式,这种行为是通过设计进行的。接口未定义实现,因此Ilocation不会“继承” Ilocationsource的属性。相反,ILocation仅定义具体实施必须实施的内容。
有关定义此行为的CLI(通用语言基础架构)规范的全部详细信息,请查看: http://haacked.com/archive/2009/11/11/interface-inheritance-esoterica.aspx
其他提示
我只需引用Codeplex中发布的ASP.NET MVC2源代码即可。我做到了,这很简单。
当您通过源代码调试时,它将为您提供更好的理解。