Search code examples
razorumbracoumbraco7archetypes

How to use Archetype with Razor?


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?


Solution

  • 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/