Search code examples
c#dotnetnuke2sxc

2sxc Get list of fields of an entity


Let's say I have an entity "Cars" with two fields "Brand" and "Model".

In a c# template, is it possible to dynamically get the name of the fields inside "Cars" to a list? The output would meed to be {"Brand", "Model"}.

Even further, is it possible to get the description and a specific translation of the field name and description?


Solution

  • Using Daniel's comments and circling around to just answer your original question, here it is simplified and things are split up a little to see the parts:

    @inherits ToSic.Sxc.Dnn.RazorComponent
    @using Newtonsoft.Json
    
    @{
        var myData = AsList(Data);
        var myDatum = AsEntity(myData.First());
        var myFieldNames = (myDatum.Type.Attributes as IEnumerable<dynamic>).Select(a => a.Name);
    
    }
    <pre>
    myDatum.Type.Name = @myDatum.Type.Name
    myFieldNames = @JsonConvert.SerializeObject(myFieldNames)
    </pre>
    

    Which then outputs just:

    myDatum.Type.Name = Cars
    myFieldNames = ["Name","Brand","Model"]