Kentico Xperience 13 documentation and ASP.NET Core

Most documentation about running Xperience applications under ASP.NET Core can be found in a dedicated section: Developing Xperience applications using ASP.NET Core. The rest of the documentation still applies, but some code samples and scenarios might need slight modifications for Core projects.

Certain pages allow you to switch between Core and MVC 5 content using a selector located under the page heading.

All major differences between the MVC 5 and Core platforms are summarized in Migrating to ASP.NET Core.

×

Defining 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.

Creating 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 the ~/Models/Sections/<section name> folder.
We recommend storing section property models in a dedicated <SectionName> folder together with other files required by the section.

Defining 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:

    • Decorate the appropriate properties using the EditingComponent attribute (available in the Kentico.Forms.Web.Mvc namespace).

    • The attribute assigns and configures a form component, which is used as the input element for the given property in the configuration dialog.

      Note: To learn about the available options when using and configuring editing components for properties, see Assigning editing components to properties.

    Example - Setting an editing component
    [EditingComponent(TextInputComponent.IDENTIFIER, 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 that restrict how and when properties are displayed in the section configuration dialog.
  • If you need to create advanced property editors, you can implement modal dialogs. This allows users to set the section property's value in a custom dialog (a new window separate from the configuration dialog).

Handling 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 Developing page builder sections for more information).

Basic sections

For sections without a custom controller 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's default controller 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.

Example
@using Kentico.PageBuilder.Web.Mvc
@using Kentico.Web.Mvc

@model ComponentViewModel<CustomSectionProperties>

<div style="background-color: @Model.Properties.Color;">
    @Html.Kentico().WidgetZone()
</div>

Sections with a custom controller

To work with properties in your section controllers, the controller class must inherit from the generic SectionController<TPropertyModel> base class, with the appropriate property model class as the generic type parameter.

public class CustomSectionController : SectionController<CustomSectionProperties>

Retrieve the properties as a strongly typed object via the IComponentPropertiesRetriever service (Kentico.PageBuilder.Web.Mvc namespace) and its Retrieve<TSectionProperties> method. 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.

// Contains an instance of the IComponentPropertiesRetriever service. For example, obtained via dependency injection.
private readonly IComponentPropertiesRetriever componentPropertiesRetriever;

// Gets the value of a section property (e.g. within the Index action of the section's controller)
CustomSectionProperties properties = componentPropertiesRetriever.Retrieve<CustomSectionProperties>();
string propertyValue = properties.Color;

You can then adjust the code of your controller 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.

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.


Accessing section properties in POST actions

Section properties cannot be by default retrieved during POST requests. Such requests lack the context of the current page and section instance.

To retrieve section properties in POST actions, 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 within the given form tag in your section view.

Example
using System.Web.Mvc.Ajax
using Kentico.Web.Mvc
using Kentico.PageBuilder.Web.Mvc

...

@using (Ajax.BeginForm("PostAction", "CustomSection", null, new AjaxOptions
{
    HttpMethod = "POST",
    UpdateTargetId = "sectionForm"
}, new { id = "sectionForm" }))
{
    @Html.Kentico().AntiForgeryToken()
    @Html.Kentico().ComponentPropertiesData()
	...
    <input type="submit" value="Submit" />
}

The method renders a hidden field that persists the section's properties and makes them available via the properties retriever in controller 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 - Developing a section with a configurable property

The following scenario 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).

Note: The following example is based on the LearningKit project. To use the code samples in your project, you need to modify the namespaces, identifiers and other occurrences where LearningKit is mentioned to match your project's name.

Property model

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

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

namespace LearningKit.Models.Sections.CustomSection
{
    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 ~/Views/Shared/Sections folder:

@using Kentico.PageBuilder.Web.Mvc
@using Kentico.Web.Mvc

@using LearningKit.Models.Sections.CustomSection

@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;">
    @Html.Kentico().WidgetZone()
</div>

Section registration

Register the section into the system using the RegisterSection assembly attribute. We recommend adding a dedicated code file to your project's ~/App_Start folder for the purposes of component registration, for example named PageBuilderComponentRegister.cs.

using LearningKit.Models.Sections.CustomSection;

using Kentico.PageBuilder.Web.Mvc;

// Registers the 'Custom section' section (it uses the system's default controller and ComponentViewModel)
[assembly: RegisterSection("LearningKit.Sections.CustomSection",
						  "Custom section",
                          typeof(CustomSectionProperties),
                          customViewName: "Sections/_CustomSection",
                          IconClass = "icon-square")]

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).

Note: The following example is based on the LearningKit project. To use the code samples in your project, you need to modify the namespaces, identifiers and other occurrences where LearningKit is mentioned to match your project's name.

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")]


Was this page helpful?