How to retrieve the value of a Dropdown list field

Tomislav Stipić asked on June 2, 2021 18:03

Hello..

How can I retrieve the values of a dropdown list?

I have a dropdown field named "DropDownField" on a page. Its choices are:

  1. ValueIWant0;VisibleDropDownValue0
  2. ValueIWant1;VisibleDropDownValue1

Using CurrentDocument.GetStringValue("DropDownField", null); the values I received are either "VisibleDropDownValue0" or "VisibleDropDownValue1". How do I reach the values "ValueIWant0" and "ValueIWant0"?

Recent Answers


Dmitry Bastron answered on June 4, 2021 11:16

Hi Tomislav,

Could you please provide more context regarding your task?

  • Kentico CMS version, and whether it's Portal Engine or MVC.
  • Where have you configured this drop-down? On the page type, or form, or somewhere else?
  • Please describe in more details what you are trying to achieve.
0 votesVote for this answer Mark as a Correct answer

Tomislav Stipić answered on June 4, 2021 14:41 (last edited on June 4, 2021 14:47)

Hey Dmitry.

  • The Kentico version is 11.
  • I have configured this dropdown as a field of a page type.
  • I am trying to achieve that when a content editor picks one of the options from the dropdown field, that the value attached to it is provided to the web-part which is present on all of the pages of that page type.

What I mean by "value provided".

My dropdown field has the following in the dropdown options:

valueProvided0;ValueVisibleToTheContentEditor0

valueProvided1;ValueVisibleToTheContentEditor1

The values visible for selection by the content editor in the dropdown field are "ValueVisibleToTheContentEditor0" and "ValueVisibleToTheContentEditor1", while the values I want to access are the values "valueProvided0" and "valueProvided1".

Here is a screenshot of what I'm trying to describe: https://prnt.sc/140eqsm

0 votesVote for this answer Mark as a Correct answer

Dmitry Bastron answered on June 4, 2021 15:34

Hi Tomislav,

Ok, it makes more sense now I think, thanks. You want your webpart to display to the website user "ValueVisibleToTheContentEditor0" or "ValueVisibleToTheContentEditor1", right?

Unfortunately, it's not possible. When content editors select something from this drop-down, CMS writes "valueProvided0" in the database. Then, when the webpart retrieves this page from the database, it gets only what's stored in the database - "valueProvided0". It doesn't even know that page type has some field display config for this drop-down. There are a couple of options how to implement it:

First. Duplicate this configuration for the web part as well (either hardcode or in config) so that web part can resolve "valueProvided0" into "ValueVisibleToTheContentEditor0". This is the simplest but not recommended route.

Second. Use localization macros. Change page type config like this:

valueProvided0;{%valueProvided0%}
valueProvided1;{%valueProvided1%}

Then add valueProvided0 and valueProvided0 localization strings. In this case you would be able to get this localization string by calling ResHelper.GetString("valueProvided0") as kind of translation.

Third. Create a separate "dictionary" table that will be storing these values as key-value. It could be module custom class, or custom table. In this case you could use this dictionary table for both page type drop-down config (with SQL query for example) and within web part you would be able to query the value by the stored key.

0 votesVote for this answer Mark as a Correct answer

Tomislav Stipić answered on June 7, 2021 10:20 (last edited on June 7, 2021 10:22)

Hey Dmitry,

Thank you on the time you've put into typing all of that. I want to tell you that my issue was one solvable by a restart of the Kentico application. I've worked on a development server instance and a local instance at the same time. I'd changed the dropdown field on the development server, while I ran the testing for the new functionality locally. My local Visual Studio project was receiving the values "VisibleDropDownValue0" and "VisibleDropDownValue1" no matter how I'd inputted the list of options into the dropdown field. After I'd restarted the application, my Visual Studio project finally started receiving the "valueProvided0" and " valueProvided1" values.

0 votesVote for this answer Mark as a Correct answer

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