CategorySelectorDropDownComponent.cs
using Intranet.Models.FormComponents;
using Kentico.Forms.Web.Mvc;
using CMS.Helpers;
using CMS.Taxonomy;
[assembly: RegisterFormComponent("CategorySelector", typeof(CategorySelectorDropDownComponent), "Drop-down with custom data", IconClass = "icon-menu")]
namespace Intranet.Models.FormComponents
{
public class CategorySelectorDropDownComponent : SelectorFormComponent<CategorySelectorDropDownComponentProperties>
{
protected override IEnumerable<SelectListItem> GetItems()
{
DataSet ds = CategoryInfoProvider.GetCategories("CategoryParentID in (9, 10) ", "CategoryParentID, CategoryOrder", 0, "CategoryID, CategoryParentID, CategoryDisplayName, CategoryOrder, CategoryLevel, CategorySiteID, CategoryEnabled");
if (!DataHelper.DataSourceIsEmpty(ds))
{
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
var listItem = new SelectListItem()
{
Value = ValidationHelper.GetInteger(ds.Tables[0].Rows[i]["CategoryID"], 0).ToString(),
Text = ValidationHelper.GetString(ds.Tables[0].Rows[i]["CategoryDisplayName"], "")
};
yield return listItem;
}
}
}
}
}
CategorySelectorDropDownComponentProperties.cs
using Kentico.Forms.Web.Mvc;
namespace Intranet.Models.FormComponents
{
public class CategorySelectorDropDownComponentProperties : SelectorProperties
{
}
}
_CategorySelectorDropDownComponent.cshtml
@using Kentico.Forms.Web.Mvc
@using Intranet.Models.FormComponents
@model CategorySelectorDropDownComponent
@{
var htmlAttributes = ViewData.GetEditorHtmlAttributes();
}
@* Invoking a Get operation on the component's 'Items' property executes the GetItems method. *@<br/>
@Html.DropDownListFor(x => x.SelectedValue, Model.Items, null, htmlAttributes)
Widget properties i added the following code
[EditingComponent("CategorySelector")]
public string CustomProperty { get; set; }