[英]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). 如果我將 HostedControls 的類型更改為 ConcreteControlText[],它會綁定。 我想將它保留為 AbstractControl[] 以便我可以托管多種類型的控件。
我知道抽象綁定器正在工作,因為 MainModel.Controls 綁定。
在調試ConcreteControlHost的綁定時,binder._propertyBinders有一個HostedControls的入口,如下:
{[ModelMetadata (Property: 'ConcreteControlHost.HostedControls' Type: 'AbstractControl[]'), {Microsoft.AspNetCore.Mvc.ModelBinding.Binders.ArrayModelBinder<MyProject.AbstractControl>}]}
在該屬性之后 (Value.ElementBinder.Inner)最終導致 AbstractModelBinder。
AbstractModelBinder 中的斷點在綁定 ConcreteControlHost 的屬性時不會被命中,但在綁定 MainModel 的屬性時會被命中(例如,我得到了 ConcreteControlHost 的命中,但沒有命中 ConcreteControlText)。
這與在編輯器模板中調用具有相同 Model 的另一個編輯器模板無關,因為它不是相同的 model,並且因為一切都正確呈現,所以它只是不綁定。 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; }
}
有誰看到我需要更改以允許模型綁定在 ConcreteControlHost.HostedControls 上工作?
結果在我的實際代碼中,HostedControls 有{ get; private set; }
{ get; private set; }
{ get; private set; }
。 刪除 setter 上的 private 修飾符使其工作。
傑里米,感謝您對此進行調查。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.