Pass Entity

Feb 7, 2014 at 12:21 AM
I have 2 content types that are related through an entity field.

In Razor I access the object using
var otherContent = Content.RelatedInfo[0]
This works well and I can access the values in the other content.
I have a
@helper doStuff(???){} 
I want to pass the otherContent object to the doStuff() helper. What type should be declared for the parameter?
Coordinator
Feb 7, 2014 at 8:53 AM
Just do a
@otherContent.GetType()
to find the returned type
GetType()
Feb 7, 2014 at 3:36 PM
I appreciate this suggestion but it doesn't quite work with 2Sexy Content Types, or I don't know how to properly use the type returned.

@otherContent.GetType() always returns "ToSic.SexyContent.DynamicEntity".

When using this type in the parameter of the helper such as:
@helper doStuff(ToSic.SexyContent.DynamicEntity passedObject)
{
   <div>@passedObject.myField</div>
}
it causes an error because myField is not recognized.
This is totally reasonable as it is not the type I want it to be, it is a "ToSic.SexyContent.DynamicEntity" type.
I could also get the data there through a general Object but I am unsure of how to cast this back so I can access fields.

In case it isn't clear what I mean by this, the similar equivalent that I know works with a string instead of the 2Sexy Content Type is:
    Object s;
    string z = "blah";
    s = z;
    string b = (string)s;
I feel like I am missing something like:
@helper doStuff(ToSic.SexyContent.DynamicEntity passedObject)
{
    var myObject = (MyContentType) passedObject;
   <div>@myObject.myField</div>
}
How can I make the fields inside the DynamicEntity accessible from a helper?
Developer
Feb 10, 2014 at 6:15 AM
Edited Feb 10, 2014 at 6:16 AM
A Content object is technically of type ToSic.SexyContent.DynamicEntity. But it is declared as dynamic, which means that the properties you are accessing of it will be resolved at runtime. There is no type like MyContentType for each Content type.

Use the dynamic keyword:
@helper doStuff(dynamic passedObject)
{
    <div>@passedObject.myField</div>
}
Hope you get it working.

Raphael
Marked as answer by RacerNerd on 2/10/2014 at 8:57 AM
Feb 10, 2014 at 3:59 PM
Thanks for the suggestion. This worked well.