Can I get items in @Content without naming them

Sep 13, 2015 at 12:30 PM
Edited Sep 13, 2015 at 12:34 PM
Hi

How can I accomplish something like this

I am working on a datatable with clientside filtering and sorting and i have pretty much all of the admin done, but i want to make it as easy for the final admin so instead of him having to do the razor code I was thinking of doing something like this

one arrary for the visible columns so lets say Name, Descripton, Image
and another array for the invisible columns say filter1,filter2,filter3

so the I can create td elements for the visibel columns and data attibutes for the invisible columns so how would i rewrite this
@foreach (var e in List)
                    {
                        var Content = e.Content;
                        var cid = Content.EntityId;

                        <tr class="tbl-item" data-filter="@Content.filter1">
                            <td class="title desc">
                                @Content.Name
                                
                            </td>
so in the above code I would like to check for each of the @Content in arry visible then create the td and for each of the @Content in array invisible create a dataattribute with the correct name and value

Is this possible,can I get the actual fieldname of the @Content without naming it like @Content.fieldname
Coordinator
Sep 14, 2015 at 8:17 AM
If I correctly understand you, you would like to know if you could access the content properties in another way - like in a dictionary.

Yes, you can.

In most cases we're casting the object to AsDynamic(...) which gives us the comfort functionality, nice names, simplified language resolution etc.

The dynamic kind of creates multiple layers of abstraction
  1. provide a dynamic - which is a "nice syntax" allowing you to write @Content.Title - this is called an ExpandoObject in .net
  2. provide a dictionary of values - which the dynamic will then use to resolve everything behind the . (for the expando object)
  3. use the "getbestvalue()" method to get the value (to handle all language aspects)
In your case, you want to go deeper - so you'll need to work with the dictionary (#2) or with the original entity.

To work with it as a dictionary of attributes, I think you can cast it - I'm not sure to what but probably dictionary<string, object> or something. Haven't done it for a while but that should work.

To work with the more complex data you'll need to make an var c = AsEntity(Content).

The entity-structures are not well documented, because they are not "official". Because anything "official" we have to keep stable across versions - even if we find a better way to do it. But your case is simple.

Bascially the AsEntity returns an IEntity (https://github.com/2sic/eav/blob/master/ToSic.Eav/Interfaces/IEntity.cs). This contains a list of Attributes. You can go through that, and you can also work with the entity GetBestValue. This does a bunch of checking in the background (like getting the EntityGuid, language resolution etc.)

So if you want the Entity way, your code will probably have some components of:
var e = AsEntity(Content);
var attList = e.Attributes; // the dictionary with all alltributes

var val1 = e.GetBestValue("Title");
var valLink = e.GetBestValue("Image", true); // also resolve links, so convert "File:27" into a real url
var val2 = e.GetBestValue(attList[0].Name); // get the best value of the first attribute
Sep 14, 2015 at 3:27 PM
Thanks

looks to be the solution I need ill keep you updated

Armand