3

I can't believe I couldn't find examples online.

This is my simple Archetype. My Archetype

This is what I tried:

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

but it gives this error:

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

EDIT: This works:

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

Wondering if there is any shorter way of writing it?

Aximili
  • 28,626
  • 56
  • 157
  • 216

2 Answers2

4

Well, no - an Archetype property can have a complex, nested set of data quite often in collections which may also be nested. In fact, it's quite common to use a corresponding nested set of partial views just to render it out correctly if you have for example nested Archetype properties (it happens).

There are some tutorials/samples available for this sort of thing:

There are also other packages designed to help you map Archetype properties to POCO as well - e.g. https://our.umbraco.org/projects/developer-tools/archetype-mapper/

Robert Foster
  • 2,317
  • 18
  • 28
3

I prefer the typed way of getting the properties.

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");
    }
}
NinjaOnSafari
  • 998
  • 1
  • 8
  • 32
  • The typed way is my preferred starting point too; however you still need to iterate through and if you have nested archetype properties you'll most probably want to use some sort of hierarchical set of partial views. – Robert Foster Jan 08 '16 at 06:29