需要从实体中获取 2sxc 字段类型
Need to get the 2sxc Field Type from an Entity
在解决上一个问题 的所有内容后,我想对其进行清理并使其更加 useful/reusable。到目前为止,我正在决定 how 到 copy/add 使用内容类型字段 names 的字段,所以 attribute.Key 里面属性上的foreach。我需要的是知道实体字段的类型;表示字符串、数字、超链接、实体等
所以我想要类似 if(AsEntity(original).FieldType == "HyperLink") { do this stuff } 的东西。我已经浏览了 API 文档,但没有发现如何获取信息。可能吗?
我确实发现 attribute.Value 有一个我可以用来回答其中大部分问题的类型,但是超链接和字符串都显示了,System.String。
以下依次为字符串、超链接、实体和数字:
atts: ToSic.Eav.Data.Attribute`1[System.String]
atts: ToSic.Eav.Data.Attribute`1[System.String]
atts: ToSic.Eav.Data.Attribute`1[ToSic.Eav.Data.EntityRelationship]
atts: ToSic.Eav.Data.Attribute`1[System.Nullable`1[System.Decimal]]
那么,是否有一种方法可以从实体或其属性或 object/methods/properties 的其他途径中获取字段类型名称的答案?或者是否有某种我可以获得的包装器可以让我处理 (convert to/from) 超链接?我对其他想法持开放态度。由于 fields.Add()
与“FieldType”不同,这将非常有用。
这有点简单,但由于 Razor 的动态特性,需要多一点代码。这是一个示例代码,可以满足您的需要:
@using System.Collections.Generic;
@using System.Linq;
@using ToSic.Eav.Data;
var type = AsEntity(Content).Type;
var attributes = type.Attributes as IEnumerable<IContentTypeAttribute>;
var typeOfAwards attributes.First(t => t.Name == "Awards").Type; // this will return "Entity"
我在这里为您创建了一个快速示例:https://2sxc.org/dnn-tutorials/en/razor/data910/page
在解决上一个问题
所以我想要类似 if(AsEntity(original).FieldType == "HyperLink") { do this stuff } 的东西。我已经浏览了 API 文档,但没有发现如何获取信息。可能吗?
我确实发现 attribute.Value 有一个我可以用来回答其中大部分问题的类型,但是超链接和字符串都显示了,System.String。
以下依次为字符串、超链接、实体和数字:
atts: ToSic.Eav.Data.Attribute`1[System.String]
atts: ToSic.Eav.Data.Attribute`1[System.String]
atts: ToSic.Eav.Data.Attribute`1[ToSic.Eav.Data.EntityRelationship]
atts: ToSic.Eav.Data.Attribute`1[System.Nullable`1[System.Decimal]]
那么,是否有一种方法可以从实体或其属性或 object/methods/properties 的其他途径中获取字段类型名称的答案?或者是否有某种我可以获得的包装器可以让我处理 (convert to/from) 超链接?我对其他想法持开放态度。由于 fields.Add()
与“FieldType”不同,这将非常有用。
这有点简单,但由于 Razor 的动态特性,需要多一点代码。这是一个示例代码,可以满足您的需要:
@using System.Collections.Generic;
@using System.Linq;
@using ToSic.Eav.Data;
var type = AsEntity(Content).Type;
var attributes = type.Attributes as IEnumerable<IContentTypeAttribute>;
var typeOfAwards attributes.First(t => t.Name == "Awards").Type; // this will return "Entity"
我在这里为您创建了一个快速示例:https://2sxc.org/dnn-tutorials/en/razor/data910/page