简体   繁体   English

Model 抽象 class 引用抽象 class Z1848E732vc14CCE460F9B24F0C80F57 核心

[英]Model binder for abstract class reference within abstract class asp.net core mvc 3

I'm following along at Model binder for abstract class in asp.net core mvc 2 , but my model doesn't bind the HostedControls in ConcreteControlHost (see below). I'm following along at Model binder for abstract class in asp.net core mvc 2 , but my model doesn't bind the HostedControls in ConcreteControlHost (see below). If I change the type of HostedControls to ConcreteControlText[], it does bind.如果我将 HostedControls 的类型更改为 ConcreteControlText[],它会绑定。 I'd like to keep it as AbstractControl[] so that I can host multiple types of control.我想将它保留为 AbstractControl[] 以便我可以托管多种类型的控件。

I know the abstract binder is working because MainModel.Controls binds.我知道抽象绑定器正在工作,因为 MainModel.Controls 绑定。

While debugging the binding of ConcreteControlHost, binder._propertyBinders has an entry for HostedControls as follows:在调试ConcreteControlHost的绑定时,binder._propertyBinders有一个HostedControls的入口,如下:
{[ModelMetadata (Property: 'ConcreteControlHost.HostedControls' Type: 'AbstractControl[]'), {Microsoft.AspNetCore.Mvc.ModelBinding.Binders.ArrayModelBinder<MyProject.AbstractControl>}]} Following that property (Value.ElementBinder.Inner) eventually leads to AbstractModelBinder. {[ModelMetadata (Property: 'ConcreteControlHost.HostedControls' Type: 'AbstractControl[]'), {Microsoft.AspNetCore.Mvc.ModelBinding.Binders.ArrayModelBinder<MyProject.AbstractControl>}]}在该属性之后 (Value.ElementBinder.Inner)最终导致 AbstractModelBinder。

Breakpoints in AbstractModelBinder are not hit when binding the properties of ConcreteControlHost, but are when binding the properties of MainModel (as in, I get the hits for ConcreteControlHost, but not for ConcreteControlText). AbstractModelBinder 中的断点在绑定 ConcreteControlHost 的属性时不会被命中,但在绑定 MainModel 的属性时会被命中(例如,我得到了 ConcreteControlHost 的命中,但没有命中 ConcreteControlText)。

This isn't related to In an Editor Template call another Editor Template with the same Model because it isn't the same model, and because everything renders correctly, it just doesn't bind.这与在编辑器模板中调用具有相同 Model 的另一个编辑器模板无关,因为它不是相同的 model,并且因为一切都正确呈现,所以它只是不绑定。 None of the ConcreteControlTexts referenced by HostedControls are referenced directly by MainModel.Controls. HostedControls 引用的 ConcreteControlTexts 都不是 MainModel.Controls 直接引用的。

public class MainModel {
   public AbstractControl[] Controls;
}

public abstract class AbstractControl {
   public string TypeName { get; set;}
}

public class ConcreteControlText: AbstractControl {
   public string Text { get; set; }
}

public class ConcreteControlHost: AbstractControl {
   public AbstractControl[] HostedControls { get; set; }
}

Does anyone see what I need to change to allow model-binding to work on ConcreteControlHost.HostedControls?有谁看到我需要更改以允许模型绑定在 ConcreteControlHost.HostedControls 上工作?

Turns out in my actual code, HostedControls had { get; private set; }结果在我的实际代码中,HostedControls 有{ get; private set; } { get; private set; } { get; private set; } . { get; private set; } Removing the private modifier on the setter made it work.删除 setter 上的 private 修饰符使其工作。

Jeremy, thanks for looking into this.杰里米,感谢您对此进行调查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM