Section properties

When developing Page Builder sections, you can define properties that allow content editors to adjust the appearance or behavior of the sections in the administration interface. Users then interact with the section properties through section configuration dialogs.

Use the following process to develop properties for a section:

  1. Create a model class that defines the section properties
  2. Define the configuration dialog to allow content editors to modify the properties
  3. Handle the properties in the section’s code

See the Example on this page for a scenario with full code samples.

Create property models

The properties of a section must be defined within a model class that implements the ISectionProperties interface (available in the Kentico.PageBuilder.Web.Mvc namespace).

Specify each section property by creating a corresponding property in the model class. You can also set default values for the properties.

Example


public class CustomSectionProperties : ISectionProperties
{
    // Defines a property and sets its default value
    public string Color{ get; set; } = "#FFF";
}

You can use the Newtonsoft.Json.JsonIgnore attribute to exclude dynamically computed section properties from database serialization.

We recommend storing section property models in a dedicated <SectionName> folder together with other files required by the section.

Define the configuration dialog

The configuration dialog is a simple way to allow content editors to set values for section properties. In the property model class, you need to define editing form components for section properties which you want to make editable in the configuration dialog. You can use the system’s default form components or develop custom form components.

  1. Edit the section’s property model class in your live site project.

  2. Define the visual interface of the configuration dialog by assigning appropriate editing components to the configuration properties.

    Example - Setting an editing component
    
    
     [TextInputComponent(Order = 0, Label = "Color")]
     public string Color { get; set; } = "#FFF";
    
     

Users can now click the Configure ( ) icon when working with sections in the administration interface. This opens the section properties dialog, and the configured property values affect the appearance and functionality of the section on the live site.

Advanced property options

You can add dynamic visibility conditions and validation that restrict how and when properties are displayed in the section configuration dialog.

Handle properties in section code

In order for properties to have an effect on a section’s appearance or functionality, you need to retrieve the property values and adjust the section’s output code or logic correspondingly. The required steps depend on the development approach used to create the section (see Sections for Page Builder for more information).

Basic sections

For basic sections that consist only of a partial view file and a properties class, handle the property values in the section’s partial view.

The view must use the generic ComponentViewModel<TPropertyModel> class as its model, with the appropriate property model class as the generic type parameter. The system ensures that the property values configured for the currently processed section are passed to the view. Retrieve the property values from the model’s Properties  member, which returns an object of the specified property model class.



@addTagHelper Kentico.Content.Web.Mvc.WidgetZoneTagHelper, Kentico.Content.Web.Mvc

@using Kentico.PageBuilder.Web.Mvc

@model ComponentViewModel<CustomSectionProperties>

@* Shows a sample section with a background color specified in the section's configuration dialog *@
<div style="background-color: @Model.Properties.Color;">
    @* Renders a widget zone via the widget-zone Tag Helper *@
    <widget-zone />
</div>

Sections based on a view component

To work with properties in your section view components, the component’s Invoke or InvokeAsync method signature must declare the ComponentViewModel<TPropertiesType> parameter, with the appropriate property model class as the generic type.



// The signature of a view component's InvokeAsync method for sections with custom properties
public Task<IViewComponentResult> InvokeAsync(ComponentViewModel<CustomSectionProperties> sectionProperties)

The section’s properties are accessible via the Properties property of the ComponentViewModel parameter. The method returns an object of the specified property model class. The object’s property values are loaded from the current configuration of the processed section.



// Gets the value of a section property from within a component's Invoke method
var propertyValue = sectionProperties.Properties.BackgroundColor;

You can then adjust the flow of your code based on the values of individual section properties, or pass them to the section’s view using an appropriate view model.

Do not directly pass the property model to your section views. We strongly recommend creating a separate view model class, which you can then use to pass data to the section view.

Access section properties in POST actions

Basic POST requests do not by default contain section properties data. To access the properties of a section during POST actions (in the controller class handling POST requests), you first need to include the properties into the data submitted by the corresponding HTML form in the section’s output. Call the Html.Kentico().ComponentPropertiesData extension method (or its Tag Helper equivalent) within the given form tag in your section view.

Example


using Kentico.Content.Web.Mvc

...

<form asp-controller="SectionPostController" asp-action="HandlePost" method="post">
    ...

    @Html.Kentico().ComponentPropertiesData()

    <input type="submit" value="Submit" />
</form>

The method renders a hidden field that persists the section’s current properties configuration.

On the server, access the section’s properties in the corresponding controller class via the IComponentPropertiesRetriever service and its Retrieve<TSectionProperties> method. Specify the section’s properties class as  the method’s generic parameter: 

Controller class handling the request


// Contains an instance of the IComponentPropertiesRetriever service (e.g., obtained via dependency injection)
private readonly IComponentPropertiesRetriever componentPropertiesRetriever;

// Gets the properties of the section as a strongly typed object 
MySectionProperties properties = componentPropertiesRetriever.Retrieve<MySectionProperties>();

Example - Develop a section with a configurable property

The following section will guide you through a step-by-step process of developing a simple Page Builder section with a configurable property.

When finished, the section is displayed with a color background of your choice. The color is set via a section property and can be modified through a section configuration dialog. The color can be specified in any text format that is accepted by the CSS background-color property (e.g. a HEX or RGB value).

Property model

Create a properties model CustomSectionProperties.cs in the ~/Components/PageBuilder/Sections/CustomSection folder:



using Kentico.Forms.Web.Mvc;
using Kentico.PageBuilder.Web.Mvc;

public class CustomSectionProperties : ISectionProperties
{
    // Defines a property and sets its default value
    // Assigns the default Xperience text input component, which allows users to enter
    // a string value for the property in the section's configuration dialog
    [EditingComponent(TextInputComponent.IDENTIFIER, Order = 0, Label = "Color")]
    public string Color { get; set; } = "#FFF";
}

Partial view

Create a partial view _CustomSection.cshtml in the ~/Components/PageBuilder/Sections/CustomSection folder:



@addTagHelper Kentico.Content.Web.Mvc.WidgetZoneTagHelper, Kentico.Content.Web.Mvc

@using Kentico.PageBuilder.Web.Mvc

@model ComponentViewModel<CustomSectionProperties>

@* Shows a sample section with a background color specified in the section's configuration dialog *@
<div style="background-color: @Model.Properties.Color;">
    @* Renders a widget zone via the widget-zone Tag Helper *@
    <widget-zone />
</div>

Section registration

Register the section into the system using the RegisterSection assembly attribute. We recommend adding a dedicated code file to your project for the purposes of component registration, for example named ComponentRegister.cs.



[assembly: RegisterSection("MyCompany.Sections.CustomSection",
                           "Custom section",
                           typeof(CustomSectionProperties),
                           customViewName: "~/Components/PageBuilder/Sections/CustomSection/_CustomSection.cshtml",
                           IconClass = "icon-square")]