Custom Table Dropdown List in Contact

Ivan Louw asked on April 4, 2019 08:20

Hi,

We use Kentico 12 MVC.

I managed to add a new field to Contact management - Contact class. The new field is a drop down which selects it values from a custom table.

The SQL query is SELECT [Code] ,[DisplayText] FROM [TestDb].[dbo].[customtable_Testlist] WHERE [TestCode] = 'Test1' ORDER BY [TestCode] DESC

The Item Transformation and Value properties are not set. (Although I did add values to to them, which did not deliver the desired result.)

The field works fine when editing the contact and save the code to the database. However when Displaying the contract, the new field is displayed but with the code and not the display value.

Where must I change it in Kentico to use the value instead of the code when viewing the profile of a contact in contact management.

Thanks for any advice. Ivan

Recent Answers


Roman Hutnyk answered on April 4, 2019 11:36 (last edited on April 4, 2019 11:36)

Ivan,

You're seeing code, because it is specified in your drop down to use code as a value, so this is what is stored into a database.

In order to get display name you need to get display name from a custom table by code and substitute code with it.

How to achieve this? - I'm not sure this will work with Contacts interface, but this is the approach that works with custom modules: you need to implement extender method that will translate a code into display name. Here is code sample from documentation:

/// <summary>
    /// Handles the Unigrid control's OnExternalDataBound event.
    /// </summary>
    private object Control_OnExternalDatabound(object sender, string sourceName, object parameter)
    {
        if (sourceName == "officehours")
        {
            // Gets the ID of the time zone assigned to the office
            int timeZoneId = ValidationHelper.GetInteger(parameter, 0);

            // Loads a TimeZoneInfo object representing the office's time zone
            CMS.Globalization.TimeZoneInfo timeZone = TimeZoneInfoProvider.GetTimeZoneInfo(timeZoneId);

            // Gets the current time, converted to the office's time zone
            DateTime currentDateTime =
                TimeZoneHelper.ConvertTimeZoneDateTime(DateTime.UtcNow, TimeZoneInfoProvider.GetTimeZoneInfo("GMT_UTC"), timeZone, false);

            // Formats the office's local time into a string
            string officeLocalTime = "(Local time: " + currentDateTime.ToString("h:mm tt") + ")";

            // Checks whether the office's local time is within office hours and displays the result in the column
            if (currentDateTime.TimeOfDay < new TimeSpan(9, 0, 0) || currentDateTime.TimeOfDay > new TimeSpan(17, 0, 0))
            {
                return "<span class=\"StatusDisabled\">" + ResHelper.GetString("general.no") + "</span> " + officeLocalTime;
            }
            else
            {
                return "<span class=\"StatusEnabled\">" + ResHelper.GetString("general.yes") + "</span> " + officeLocalTime;
            }
        }

        return parameter;
    }

More detail on implementation could be found here.

1 votesVote for this answer Mark as a Correct answer

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