2016-08-01 177 views
0

我有Parent Model類和viewModel類。 Parent Model類具有GetViewModel()方法,我試圖繼承Parent類並重寫Child類中的GetViewModel()。但是,它會引發無限的異常。覆蓋Child類中的ParentModel類方法

父ViewModel類:

public class ParentViewModel 
{ 
    public IEnumerable<string> Choices { get; set; } 
    public object Value { get; set; } 
    public IMetaField MetaField { get; set; } 
    public bool IsRequired { get; set; } 
    public bool HasOtherChoice { get; set; } 
    public string ValidationAttributes { get; set; } 
    public string RequiredViolationMessage { get; set; } 
    public string CssClass { get; set; } 
} 

父模型類:

public class ChildModel : ParentModel 
{ 
    public string CorticonEntityName { get; set; } 
    public string CorticonFieldName { get; set; } 

    public override object GetViewModel(object value, IMetaField metaField) 
    { 
     this.Value = value; 
     return (object)new ChildViewModel() 
     { 
      Choices = this.DeserializeChoices(), 
      Value = value, 
      MetaField = this.MetaField, 
      HasOtherChoice = this.HasOtherChoice, 
      IsRequired = (this.ValidatorDefinition.Required.HasValue && this.ValidatorDefinition.Required.Value), 
      ValidationAttributes = this.BuildValidationAttributesString(), 
      RequiredViolationMessage = this.ValidatorDefinition.RequiredViolationMessage, 
      CssClass = this.CssClass 
     }; 
    } 
} 

我已經創建了兩個子類ChildModel類和其繼承父lasses ChildViewModel班組長。

我想覆蓋父類的GetViewModel(),所以我完成了以下工作並完美工作。

ChildViewModel類:

public class ChildViewModel:ParentViewModel 
{ 
    public string CorticonEntityName { get; set; } 
    public string CorticonFieldName { get; set; } 
} 

ChildModel類:

public class ChildModel : ParentModel 
{ 
    public string CorticonEntityName { get; set; } 
    public string CorticonFieldName { get; set; } 

    public override object GetViewModel(object value, IMetaField metaField) 
    { 
     this.Value = value; 
     return (object)new ChildViewModel() 
     { 
      Choices = this.DeserializeChoices(), 
      Value = value, 
      MetaField = this.MetaField, 
      HasOtherChoice = this.HasOtherChoice, 
      IsRequired = (this.ValidatorDefinition.Required.HasValue && this.ValidatorDefinition.Required.Value), 
      ValidationAttributes = this.BuildValidationAttributesString(), 
      RequiredViolationMessage = this.ValidatorDefinition.RequiredViolationMessage, 
      CssClass = this.CssClass, 
      CorticonEntityName=this.CorticonEntityName, 
      CorticonFieldName=this.CorticonFieldName 
     }; 
    } 
} 

但我不」要設置子類GetViewModel(所有屬性)再次作爲父GetViewModel () 是在做。 我想重用Parent類的方法並在Child類中設置新的屬性,所以我嘗試了下面的方法,但是它拋出了無限循環。

public class ChildModel : ParentModel 
{ 
    public string CorticonEntityName { get; set; } 
    public string CorticonFieldName { get; set; } 
    CheckboxesFieldViewModel baseObject { get; set; } 

    public override object GetViewModel(object value, IMetaField metaField) 
    { 
     object objBase=this.GetViewModel(value,metaField); 
     return (object)new ChildViewModel() 
     { 
      objParent=objBase, 
      CorticonEntityName = this.CorticonEntityName, 
      CorticonFieldName = this.CorticonFieldName 
     }; 
    } 
} 

如何重新父類GetViewModel(),而無需再次設置屬性:

ChildViewModel類:

public class ChildViewModel:ParentViewModel 
{ 
    public string CorticonEntityName { get; set; } 
    public string CorticonFieldName { get; set; } 
    CheckboxesFieldViewModel baseObject { get; set; } 
} 

ChildModel類。

回答

1

您的罪魁禍首是:

object objBase=this.GetViewModel(value,metaField); 

你調用同樣的方法遞歸地一遍又一遍,吹堆棧。如果你想調用父類的方法,您需要使用base關鍵字:

object objBase = base.GetViewModel(value, metaField); 
+0

KooKzip,感謝完美的作品,但我有一個小問題。我所做的方式是正確的? –

+0

@Balu不是真的,你打破了MVC模式。視圖模型可以引用模型,而不是其他方式 –

+0

我已經使用組合,因爲我從父類繼承。 –