Hi,
Maybe a little late but I was looking for something like this as well.
My solution was to create a custom form component (link). See the example below:
Form component class:
public class CategoryDropDownComponent : FormComponent<CategoryDropDownComponentProperties, string>
{
public const string IDENTIFIER = "CategoryDropDownComponent";
[BindableProperty]
public string[] SelectedCategories { get; set; } = Array.Empty<string>();
// Disables automatic server-side evaluation for the component
public override bool CustomAutopostHandling => true;
public override string GetValue()
{
return string.Join(",", SelectedCategories);
}
public override void SetValue(string value)
{
if (!string.IsNullOrEmpty(value))
{
SelectedCategories = value.Split(',');
}
else
{
SelectedCategories = Array.Empty<string>();
}
}
public MultiSelectList Items()
{
var categories = CategoryInfoProvider.GetCategories()
.OnSite(SiteContext.CurrentSiteID)
.WhereEquals(nameof(CategoryInfo.CategoryParentID), sectorCategory.CategoryID);
var data = categories.Select(x => new
{
Name = x.CategoryDisplayName,
Guid = x.CategoryGUID.ToString(),
});
return new MultiSelectList(data, "Guid", "Name");
}
}
Form component properties:
public class CategoryDropDownComponentProperties : FormComponentProperties<string>
{
public CategoryDropDownComponentProperties()
: base(FieldDataType.Text)
{
}
[DefaultValueEditingComponent("CategoryDropDownComponent", DefaultValue = "")]
public override string DefaultValue { get; set; }
}
View:
@Html.ListBoxFor(x => x.SelectedCategories, Model.Items(), htmlAttributes)