How can I assign values to dropdown properties

Geo Neeliyara asked on May 6, 2020 22:44

Hi,

I have widget with dropdown properties (Values of this dropdown properties are from Kentico CMS category). Do you have any sample code?

I have below sample code to fill the dropdown properties [EditingComponentProperty(nameof(DropDownProperties.DataSource), ";N/A\r\n_self;Same Window\r\n_blank;New Window\r\n_parent;Parent Window")]

Is there anyway to get data from DB? Any help will be appreciated.

Regards, Geo

Correct Answer

Dmitry Bastron answered on May 7, 2020 10:01

Hi Geo,

The code you posted shouldn't work as far as I know. You can follow this approach instead:

Implement custom MVC form control with dynamic datasource following this example in documentation. In your case it will be something called "CategorySelector":

[assembly: RegisterFormComponent("CategorySelector", typeof(CategorySelectorDropDownComponent), "Drop-down with custom data", IconClass = "icon-menu")]

namespace YourNamespace
{
    public class CategorySelectorDropDownComponent: SelectorFormComponent<CategorySelectorDropDownComponentProperties>
    {
        // your implementation
    }
}

And then assign this form control to your widget property like this:

[EditingComponent("CategorySelector")]
public string CustomProperty { get; set; }
0 votesVote for this answer Unmark Correct answer

Recent Answers


Brenden Kehren answered on May 6, 2020 23:17

Check out the API examples to get data from the database.

0 votesVote for this answer Mark as a Correct answer

Geo Neeliyara answered on May 6, 2020 23:24

Hi Brenden,

Many Thanks. So is that possible to write code as below

[EditingComponentProperty(nameof(DropDownProperties.DataSource), GetCategories())]

I will add the code for GetCatgories(). If you have any other suggestion please let me know.

Regards, Geo

0 votesVote for this answer Mark as a Correct answer

Geo Neeliyara answered on May 7, 2020 22:48

Hi Dmitry,

I followed the documents. but the property is not showing on settings. Would you be able to help me to troubleshoot the issue.

Regards, Geo

0 votesVote for this answer Mark as a Correct answer

Dmitry Bastron answered on May 8, 2020 13:32

Hi Geo,

Sure, could you please post your code of form control and widget properties to look at it?

0 votesVote for this answer Mark as a Correct answer

Geo Neeliyara answered on May 8, 2020 14:19 (last edited on May 8, 2020 14:27)

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; }

0 votesVote for this answer Mark as a Correct answer

Dmitry Bastron answered on May 8, 2020 14:23 (last edited on May 8, 2020 14:23)

This bit looks fine. Could you also please post the code of widget and widget properties class where you are using this form control?

0 votesVote for this answer Mark as a Correct answer

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