简体   繁体   English

从Razor HTML中的子对象获取值

[英]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. 但是,由于子对象的属性不在超对象中,因此无法在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. 我无法将子对象的属性放在超对象中,因为我要将类转换为json,并且无法在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. 我分别需要subobject和subobject属性来代替SUBOBJECTNAMESUBOBJECTATTRIBUTE

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. 如果您没有智能感知或视图显示语法错误,则可能的问题是您没有在Views文件夹的web.config中包含类的名称空间。

Remember to open and close the view after editing the web.config for the intellisense to start working correctly. 记住在编辑web.config后打开和关闭视图,以使智能感知开始正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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