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類。
KooKzip,感謝完美的作品,但我有一個小問題。我所做的方式是正確的? –
@Balu不是真的,你打破了MVC模式。視圖模型可以引用模型,而不是其他方式 –
我已經使用組合,因爲我從父類繼承。 –