I am using ASP.NET MVC. I have a parent:
public class ReportFilterBuilderStruct
{
public string PropName { get; set; }
public string ClassName { get; set; }
public string SubListName { get; set; }
//public virtual string SecondaryPropName { get; set; }
public ReportFilterBuilderStruct(string inpPropName, string inpClassName, string inpSubListName)
{
PropName = inpPropName;
ClassName = inpClassName;
SubListName = inpSubListName;
}
}
And a child:
public class DateReportFilterBuilderStruct : ReportFilterBuilderStruct
{
public string SecondaryPropName { get; set; }
public DateReportFilterBuilderStruct(string inpPropName, string inpSecondaryPropName, string inpClassName, string inpSubListName) : base(inpPropName, inpClassName, inpSubListName)
{
SecondaryPropName = inpSecondaryPropName;
}
}
}
It will be used in my view to store some data about my model class property. editorListProp
will store both ReportFilterBuilderStruct
and DateReportFilterBuilderStruct
, that is why it is a List
of parent class objects.
var editorListProp = new List<NTStock.Models.ReportFilterBuilderStruct>();
Now i want to access them:
if (subItem.GetType() == typeof(NTStock.Models.ReportFilterBuilderStruct))
{
<div class="row align-items-center">
@Html.DisplayName(subItem.PropName)
</div>
<div class="row align-items-center mb-2">
@Html.Editor(subItem.PropName, "", new { htmlAttributes = new { @class = subItem.ClassName } })
</div>
}
else
{
<div class="row align-items-center">
@Html.DisplayName(subItem.PropName)
</div>
<div class="row align-items-center mb-2">
<div class="col-6">
From:
@Html.Editor(subItem.PropName, "", new { htmlAttributes = new { @class = subItem.ClassName } })
</div>
<div class="col-6">
Till:
@Html.Editor(subItem.SecondaryPropName, "", new { htmlAttributes = new { @class = subItem.ClassName } })
</div>
</div>
}
So the point is, that I can't access my SecondaryPropName
, even though I made sure that current subItem
is definetly of type DateReportFilterBuilderStruct
.
您必须将subItem
为DateReportFilterBuilderStruct
,例如:
((DateReportFilterBuilderStruct)subItem).SecondaryPropName
This approach would work in a dynamically typed language like Python, but not in C#. Your list is of type List<ReportFilterBuilderStruct>
. When you access an item from the list, eg list[0]
, you get back an object of type ReportFilterBuilderStruct
, even if you stored a DateReportFilterBuilderStruct
there and GetType()
correctly returns DateReportFilterBuilderStruct
.
In the else
branch you will first have to cast the item to DateReportFilterBuilderStruct
:
else
{
@{ var dateReport = (DateReportFilterBuilderStruct)subItem; }
<div class="row align-items-center">
@Html.DisplayName(dateReport.PropName)
</div>
<div class="row align-items-center mb-2">
<div class="col-6">
From:
@Html.Editor(dateReport.PropName, "", new { htmlAttributes = new { @class = dateReport.ClassName } })
</div>
<div class="col-6">
Till:
@Html.Editor(dateReport.SecondaryPropName, "", new { htmlAttributes = new { @class = dateReport.ClassName } })
</div>
</div>
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.