Search code examples
kentico

Kentico 13: Create new instance of ComponentViewModel and pass widget properties


I am converting old inline widgets from K12 content into static HTML.

To do this I am using IViewComponentHelper.InvokeAsync and need to pass in ComponentViewModel as a param but I also need to pass in the actual widget props from the inline widget.

However, ComponentViewModel.Properties is get only and ComponentViewModel.Create() doesn't offer IWidgetProperties as a parameter.

Is there any way to call InvokeAsync and pass in a ComponentViewModel with WidgetProperties?

Thanks


Solution

  • var node = TreeNode.New(); //get a real treenode or it won't work
    var args = ComponentViewModel<TPropertiesType>.Create(node, widgetProperties as TPropertiesType);    
    var result = await _viewComponentHelper.InvokeAsync(viewComponentType, args);
    

    widgetProperties are an instance of IWidgetProperties