0

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.

Zarif
  • 587
  • 1
  • 4
  • 26
  • 1
    i don't understand what you are trying to do – Ehsan Sajjad Feb 21 '18 at 10:58
  • @EhsanSajjad In the Razor page, I cannot get the object I want. I want to get `item.KpiPattern.KPIs`. Instead I can only choose `item.Pattern`. – Zarif Feb 21 '18 at 11:00
  • If you check the type of the item and the do a cast to that object: `if(item is ProcessPattern) { (ProcessPattern)item.SUBOBJECTATTRIBUTE }` – torsan Feb 21 '18 at 11:04
  • @torsan I cannot choose the subobject attributes. I can only do this: `(ProcessPattern)item.Pattern`. – Zarif Feb 21 '18 at 11:20
  • @torsan the code was wrong. I fixed it, but it is still not letting me choose the attributes: `if(item.Pattern is ProcessPattern) { (ProcessPattern)item.Pattern.Processes}` – Zarif Feb 21 '18 at 11:26
  • @torsan I think I partially solved the problem: `@if (item.Pattern is KpiPattern){ KpiPattern pt = (KpiPattern)item.Pattern; }`. Next, I will use `pt` to do the rest. However the if-statement is not correct. It's never true. – Zarif Feb 21 '18 at 12:28
  • @torsan Nevermind. It doesn't work without if-statement, either. I get the following error: `Unable to cast object of type 'KPItool.Models.Pattern' to type 'KPItool.Models.KpiPattern'` – Zarif Feb 21 '18 at 12:32

1 Answers1

0

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.

LiefdeWen
  • 586
  • 2
  • 14
  • I cannot find web.config in my Views folder or somewhere else in the solution. – Zarif Feb 21 '18 at 11:07
  • No web.config in solution? Is it a .NET Core solution? – Nirman Feb 21 '18 at 11:20
  • @Nirman Yes. Apparently, that's normal: https://stackoverflow.com/questions/42542328/web-config-missing-when-creating-asp-net-core-web-app-in-vs-2017-rc Maybe I need to edit appsettings.json, but I don't know how to do it. – Zarif Feb 21 '18 at 11:28
  • oh ok! yes, there is no web.config in .NET core.. please update your question, and/or add a tag in the post so it gets noticed to the .net core skills specialists. – Nirman Feb 21 '18 at 11:41
  • @Zarif If thts the case then you can still fully qualify the classes in the view like `@model IEnumerable` – LiefdeWen Feb 21 '18 at 12:34
  • @LiefdeWen I changed it to `@model IEnumerable`, but I still get an error: `'Pattern' does not contain a definition for 'KPIs' and no extension method 'KPIs' accepting a first argument of type 'Pattern' could be found (are you missing a using directive or an assembly reference?)` – Zarif Feb 21 '18 at 12:39
  • Are you passing a `KpiPattern` to the view, if you are then just do as I edited my answer – LiefdeWen Feb 21 '18 at 12:44
  • @LiefdeWen Sorry I don't understand. I'm passing a list of `FoundPattern` to the view. Every `FoundPattern` can contain a `KpiPattern` or a `ProcessPattern`. – Zarif Feb 21 '18 at 12:50