ASP.net MVC V2 - 调试模型绑定问题 - BUG? [英] ASP.net MVC v2 - Debugging Model Binding Issues - BUG?

查看:220
本文介绍了ASP.net MVC V2 - 调试模型绑定问题 - BUG?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有不小的困难,更要调试为什么MVC是不是在给定的情况下,我有正确的结合...

I am having more than a little difficulty trying to debug why MVC is not binding correctly in a given case I have...

基本上,我有我的行动接收一个复杂的对象反过来有一个复杂的子对象 - Activity.Location.State(凡活动是复杂的对象,该行动预计,地点是一个复杂的子对象和状态只是字符串)。

Basically, I have my action which receives a complex object which in turn has a complex child object - Activity.Location.State (Where Activity is the complex object that the action expects, Location is a complex child object and State is just a string).

现在我建立了一个测试项目,据我可以告诉正是模仿了实际情况我也有,在这个测试用例的结合作品......但在我的实际项目中,结合到活动的作品而不是位置......通过把Locaiton属性中破发点,我可以告诉大家,MVC从活动获取复杂的位置对象,但它不设置任何属性...

Now I set up a test project which as far as I can tell exactly mimics the actually scenario I have, in this test case the binding works... But in my actually project, the binding to Activity works but not to Location... By putting break points within the Locaiton property I can tell that MVC is retrieving the complex Location object from the Activity, but its not setting any of the properties...

我试图调试问题,但我需要访问MVC V2 preVIEW 2我似乎无法追查符号...我想看到它实际上做什么,一旦它翻出位置对象(某种原因,我想可能是内部故障,但吞咽除外)。

I am trying to debug the issue but I need access to the MVC v2 preview 2 symbols which I can't seem to track down... I would like to see what it is actually doing once it pulls out the location object (for some reason I think it might be failing internally but swallowing the exception).

这是什么,我可以在这里做任何想法......

Any ideas on what I could do here...

干杯
安东尼

更新:

好吧,我做了什么J.W.提出并直接引用MVC项目...

Ok I did what J.W. suggested and directly reference the MVC project...

我发现这个问题,并没有是我忽略了......至于我结果,我发现了MVC目前不支持接口继承的多层次的,当涉及到模型绑定...查看下一个非常小的差异。 ..

I found the problem and there was one very small difference that I overlooked... As I result I found out that MVC does not currently support multiple levels of INTERFACE inheritance when it comes to model binding... See the following...

//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中运行出现以下情况 -

Hence what I have found is that MVC finds the Location and reads its meta type as an ILocation, but when GetModelProperties is run within the DefaultModelBinder the following occurs -

    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不支持这种风格的继承,这我感到颇为惊讶假设。也看着它看起来像这样与V2推出的V1源 - 但V1可能没有能够支持我试图做的工作。

Hence I am assuming at this point that TypeDescriptionProvider doesn't support this style of inheritance, which i am quite surprised by. Also looking at the v1 source it looks like this was introduced with v2 - but v1 mightn't have been able to support what I am trying to do anyway.

我不会说这是一个真正的错误,但我试图与具体类取代我的接口和它工作得很好。因此,行为是不是真的是我所期望的,是有点不一致。

I wouldn't say that this is really a bug, but I tried replacing my the interfaces with concrete classes and it worked fine. Hence the behavior isn't really what I would expect and is a little inconsistent.

有什么想法?我本来以为这种继承不是很标准,但经常会出现足以被照顾。感谢您的答复。

Any thoughts??? I would have thought that this inheritance was not fairly standard but would occur often enough to be catered for. Thanks for the reply.

干杯

推荐答案

原来此行为是设计使然,由于接口继承是如何工作的。接口没有定义的实现,从而ILocation不继承ILocationSource的属性。相反,ILocation只定义了一个具体的实现必须实现。

Turns out this behavior is by design due to how interface inheritance works. Interfaces do not define implementations, thus ILocation doesn't "inherit" the properties of ILocationSource. Rather, ILocation only defines what a concrete implementation must implement.

有关的全部细节,包括CLI(通用语言基础结构)规范定义这种行为的部分,请上网:<一href=\"http://haacked.com/archive/2009/11/10/interface-inheritance-esoterica.aspx\">http://haacked.com/archive/2009/11/10/interface-inheritance-esoterica.aspx

For the full details including the section of the CLI (Common Language Infrastructure) spec which defines this behavior, check out: http://haacked.com/archive/2009/11/10/interface-inheritance-esoterica.aspx

这篇关于ASP.net MVC V2 - 调试模型绑定问题 - BUG?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆