Basic usage - As<T> extension method

For basic use of Ditto, let's start with a standard DocumentType with a few properties:

  • Title (Textstring)
  • BodyText (Richtext editor)
  • Image (Upload)

Umbraco DocType properties

Using those properties as the structure of our view-model (POCO class), we can manually create a C# class:

public class MyTextModel
{
    public string Title { get; set; }

    public string BodyText { get; set; }

    public string Image { get; set; }
}

Note: in this case the property names should correspond with the DocumentType's property aliases. (If you wish to name your properties differently, please see the documentation about the UmbracoProperty attribute.)

The C# class name (MyTextModel in this example), does not need to match with the DocumentType alias.

Now you can map your IPublishedContent (content node of that DocumentType) to your corresponding POCO model using the As<T> extension method.

var poco = Model.Content.As<MyTextModel>();

Here is an example in the context of an MVC view:

@using Our.Umbraco.Ditto
@inherits UmbracoTemplatePage
@{
    var poco = Model.Content.As<MyTextModel>();
}

<h1>@poco.Title</h1>

@poco.BodyText

<p><img src="@poco.Image" alt="[image]" /></p>

Video demonstrations