首页 > 解决方案 > 需要从实体中获取 2sxc 字段类型

问题描述

在完成上一个问题的所有工作之后,是否有办法在 Code 中克隆一个或多个实体(记录),我想清理它并使其更有用/可重用。到目前为止,我正在决定如何使用 content-type 字段名称来复制/添加字段,因此 foreach 中的 attribute.Key 位于 Attributes 上。我需要的是知道实体字段的类型;表示字符串、数字、超链接、实体等。

所以我想要 if(AsEntity(original).FieldType == "HyperLink") { do this stuff } 之类的东西。我已经探索了 API 文档,但还没有发现如何获取信息。是否可以?

我确实发现attribute.Value 有一个可以用来回答大多数问题的Type,但是Hyperlink 和String 都显示了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]]

那么有没有办法从实体或其属性或对象/方法/属性的其他路径中获取答案作为字段类型名称?或者是否有某种包装器可以让我处理(转换为/从)超链接?我对其他想法持开放态度。由于fields.Add()“FieldType”不同,这将非常有帮助。

标签: razordotnetnuke2sxc

解决方案


这有点简单,但由于 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


推荐阅读