如何在 Razor 中使用 Archetype?

How to use Archetype with Razor?

真不敢相信我在网上找不到例子。

这是我的简单原型。

这是我试过的:

<img src="@CurrentPage.ctaTopLeft.image" alt="@CurrentPage.ctaTopLeft.text">

但它给出了这个错误:

'Archetype.Models.ArchetypeModel' does not contain a definition for 'image'

编辑: 这有效:

<img src="@CurrentPage.GetPropertyValue("ctaTopLeft").Fieldsets[0].GetValue("image")" alt="@CurrentPage.GetPropertyValue("ctaTopLeft").Fieldsets[0].GetValue("text")">

想知道有没有更短的写法?

嗯,不 - 一个原型 属性 可以在集合中经常有一组复杂的嵌套数据,这些数据也可能是嵌套的。事实上,如果您有嵌套的 Archetype 属性(它发生了),使用相应的嵌套部分视图集只是为了正确渲染它是很常见的。

有一些 tutorials/samples 可用于此类事情:

还有其他包旨在帮助您将 Archetype 属性也映射到 POCO - 例如https://our.umbraco.org/projects/developer-tools/archetype-mapper/

我更喜欢输入属性的方式。

var property = Model.Contet.GetPropertyValue<ArchetypeModel>("yourArchetypePropertyAlias");

if (property != null && property.Any()) {
    foreach (var item in property) {
        var imageId = item.GetValue<int>("yourImagePropertyAlias");
        var text = item.GetValue<string>("yourTextPropertyAlias");
    }
}