微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


这篇文章主要讲解了“WPF如何实现带筛选功能的DataGrid”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“WPF...
本篇内容介绍了“基于WPF如何实现3D画廊动画效果”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这...
Some samples are below for ASP.Net web form controls:(from http://www.visualize.uk.com/resources/asp
问题描述: 对于未定义为 System.String 的列,唯一有效的值是(引发异常)。 For columns not defined as System.String, the only vali
最近用到了CalendarExtender,结果不知道为什么发生了错位,如图在Google和百度上找了很久,中文的文章里面似乎只提到了如何本地化(就是显示中文的月份)以及怎么解决被下拉框挡住的问题,谈
ASP.NET 2.0 page lifecyle ASP.NET 2.0 event sequence changed a lot since 1.1. Here is the order: App
静态声明: ' Style="position: relative" AppendDataBoundItems="True"> (无 或 空 或
以下内容是从网络上搜集资料,然后整理而来的。不当之处,请不吝指教。(The following were from network, and edited by myself. Thanks in a
Imports System Imports System.Reflection Namespace DotNetNuke '*********************************
Ok so you have all seen them: “8 million tools for web development”, “5 gagillion tools that if you