简体   繁体   中英

Get values from subobject in Razor HTML

I have a class that contains a super object. When creating this class, it will contain one of the subobjects of the superobject. However, because the attributes of the subobjects are not in the superobject, I cannot access these attributes in Razor. Can someone tell me how I can reach the subobjects' attributes? I cannot put the subobjects' attributes in the superobject, because I'm going to convert the class to json and I cannot have all attributes visible in the json.

Class:

public class FoundPattern
{
    public Pattern Pattern = new Pattern();
}

Superobject:

public class Pattern : OntologicalModel
{       
}

Subobjects:

public class KpiPattern : Pattern
{
    public List<KPI> KPIs = new List<KPI>();
}

.

public class ProcessPattern : Pattern
{
    public List<Process> Processes = new List<Process>();
}

Razor page:

@model IEnumerable<FoundPattern>
@foreach (var x in item.SUBOBJECTNAME.SUBOBJECTATTRIBUTE)
{
    // do something     
}

Instead of SUBOBJECTNAME and SUBOBJECTATTRIBUTE I need the subobject and subobject attribute, respectively.

Should be something like

@foreach (var x in Model)
{
    if(x.Pattern is ProcessPattern) { 
        foreach (var y in ((KpiPattern)x.Pattern).Processes)
        { 
            //enter code here
        }
    }
    if(x.Pattern is KpiPattern) { 
        foreach (var y in ((KpiPattern)x.Pattern).KPIs)
        { 
            //enter code here
        }
    }
}

If you don't have intellisense or the view is showing syntax errors then a possible issue is that you didn't include the namespace of your classes in your web.config in your Views folder.

Remember to open and close the view after editing the web.config for the intellisense to start working correctly.

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.

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