【问题标题】:Need to get the 2sxc Field Type from an Entity需要从实体中获取 2sxc 字段类型
【发布时间】:2021-04-07 14:44:45
【问题描述】:

在解决上一个问题Is there a way to Clone one or many Entities (records) in Code 的所有工作后,我想清理它并使其更有用/可重用。到目前为止,我正在决定 如何 使用内容类型字段 names 来复制/添加字段,因此 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”不同,这将非常有用。

【问题讨论】:

    标签: razor dotnetnuke 2sxc


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-13
      • 2013-02-07
      • 2022-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多