Select Fields

Last Updated: 2019-10-11

If you want to read more about the basic fields included in Piranha, please refer to Standard Fields.

Select Fields are an easy way to add dropdown fields based on Enums to your Content Types. This feature is build into Piranha but still requires some extra initialization to work.

Registering The Field

For Select Fields to function they must be registered for each Enum being used. This is because each implementation of the generic type SelectField<> needs to have it's own Serializer registered.

Piranha.App.Fields.RegisterSelect<MyEnum>();

An Example

Let's for example say we have a HeroRegion in our application that presents a banner at the top of the page. In this Hero we can set a Background Image, but depending on the image we want the text to be Dark or Light to provide good contrast. This is a perfect use case for adding a Select Field.

HeroStyle.cs

public enum HeroStyle
{
Dark,
Light
}

HeroRegion.cs

public class HeroRegion
{
[Field]
public ImageField BackgroundImage { get; set; }

[Field]
public StringField Title { get; set; }

[Field]
public HtmlField Body { get; set; }

[Field]
public SelectField<HeroStyle> Style { get; set; }
}

Startup.cs

App.Fields.RegisterSelect<HeroStyle>();