ASP.net MVC v2 – 调试模型绑定问题 – BUG?

我试图调试为什么MVC在一个给定的情况下没有正确绑定我有一些困难我有

基本上,我有我的操作接收一个复杂的对象,它又有一个复杂的子对象 – Activity.Location.State(其中Activity是操作期望的复杂对象,Location是一个复杂的子对象,State只是一个字符串) 。

现在我设立了一个测试项目,据我所知,在我的实际项目中,绑定到活动工作,但不是位置…通过在Locaiton属性中设置断点,我可以告诉MVC正在从Activity中检索复杂的Location对象,但是它没有设置任何属性

我正在尝试调试问题,但是我需要访问MVC v2预览2个符号,我似乎无法追踪…我想看看它实际在做什么,一旦它拉出位置对象(对于一些原因我认为它可能在内部失败,但吞咽异常)。

关于我在这里可以做什么的任何想法

干杯
安东尼

更新:

好的,我做了什么建议并直接参考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)
{
}

因此,我发现MVC找到位置并将其类型读取为一个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
    }

因此,我现在假设TypeDescriptionProvider不支持这种继承风格,我很惊讶。另外看v1版本,它看起来像是用v2引入的 – 但是v1可能无法支持我正在努力做的事情。

我不会说这真的是一个错误,但我尝试用具体的类替换了接口,并且工作正常。因此,行为并不是我期望的,并且有点不一致。

有什么想法吗???我会认为这种遗产不是相当标准的,但会经常发生足够的照顾。谢谢回复

干杯

解决方法

结果是由于接口继承如何工作,这种行为是设计的。接口不定义实现,因此ILocation不会“继承”ILocationSource的属性。相反,ILocation仅定义具体实现必须实现的内容

有关详细信息,包括定义此行为的CLI(公共语言基础结构)规范的部分,请查看:http://haacked.com/archive/2009/11/10/interface-inheritance-esoterica.aspx

相关文章

这篇文章主要讲解了“WPF如何实现带筛选功能的DataGrid”,文...
本篇内容介绍了“基于WPF如何实现3D画廊动画效果”的有关知识...
Some samples are below for ASP.Net web form controls:(fr...
问题描述: 对于未定义为 System.String 的列,唯一有效的值...
最近用到了CalendarExtender,结果不知道为什么发生了错位,...
ASP.NET 2.0 page lifecyle ASP.NET 2.0 event sequence cha...