Have widget's view access a property of the containing section

Douglas Fittipaldi asked on September 3, 2025 19:41

We have a widget that outputs some text and we have it placed in a section that has a configurable property for background color. If a dark background color is selected, you can't see the text. How would I go about checking the background color property of the section that the widget is contained in when the view is loading? Thanks for any help given.

Recent Answers


vasu yerramsetti answered on September 8, 2025 11:18 (last edited on September 8, 2025 12:16)

Understand the Page Builder hierarchy

  • In Kentico’s Page Builder, a page → contains sections → which contain widgets
  • Sections can have their own properties (like your background color)
  • Widgets don’t automatically know about section properties — you have to pass them down explicitly

Pass section property to the widget view model

  • In your section’s view component, when you render widgets, you can pass additional data (like the background color) to the widget’s view.

    public class MySectionViewComponent : ViewComponent { public IViewComponentResult Invoke(SectionViewModel sectionModel) { // Expose background color to widgets ViewData["BackgroundColor"] = sectionModel.BackgroundColor; return View(sectionModel); } }

    Image Text Then in your widget view:

@{ var backgroundColor = ViewData["BackgroundColor"]?.ToString(); var textColor = backgroundColor == "dark" ? "white" : "black"; } `<div style="color:@textColor"> @Model.Text </div>

Image Text

Note: You can refer to the Kentico Xperience 13 Dancing Goat website code for section properties (1-column section).

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.