Michelle
Michelle

Reputation: 249

Kentico - Transformations to display text value of List box or Multiple choice

This looks like a simple question but I couldn't figure out after trying for hours. For my custom page type, I have a field called "Location", which displays a list of checkboxes with location names. I checked multiple boxes, but with Eval("Location"), the display is like this: 1|3|6, but not San Francisco, San Jose, Fresno. Thanks for your help!

PS: I searched and came across this old post dated 2011. I'm not sure how to implement the API option, and the javascript option is not good, since my list is dynamic and long. Also not sure if there's different way achieving this considering I'm now using Kentico 9 and in 2016, not 2011 :d

Upvotes: 0

Views: 563

Answers (3)

Laura Frese
Laura Frese

Reputation: 331

Would it be possible for you to change the numbers to the location names in the form? For example

San Francisco;San Francisco
San Jose;San Jose
Fresno;Fresno

Then in your transformation you can do

<% Eval("Location").ToString().Replace("|",", ") %>

If you cant change the numbers and you are comfortable writing something custom, I detailed how you can get the list of options in this post.

Upvotes: 1

Enn
Enn

Reputation: 2209

It seems like the "Location" field you have is not storing the display name values, but rather some other values such as ID's which point to some other database table - this is actually a good thing, but its true that retrieving those values might be a little more difficult.

There isn't any an out of the box way to to "convert" this list of ids (1|3|6...) into actual values.

You will actually need to create your own Transformation method which could take the value of your Location field and convert it to actual values using C# and appropriate Providers (depending on where your data is stored).

If you can provide more information about how exactly your data is stored and where, I might be able to provide you with some code example.

Upvotes: 1

Brenden Kehren
Brenden Kehren

Reputation: 6117

What I typically do in this case (if it's a one off setup) is create a function right within the transformation. If you need to use this in other places, create a custom method for it and expose it via macro or transformation method.

You can add something like this (assuming your locations are in custom tables) in your transformation:

<script runat="server">
public string GetLocations()
{
    string returnValue = "";
    string[] locations = Eval<string>("Location").Split('|');
    foreach (string s in locations)
    {
        var item = CMS.CustomTables.CustomTableItemProvider.GetItem(ValidationHelper.GetInteger(s, -1), "your.CustomTable");
        if (item != null)
        {
            returnValue += string.Format("<p>{0}</p>", item.GetValue("LocationName"));
        }            
    }

    return returnValue;
}

Then use it like so:

<h2>Locations:</h2>
<%# GetLocations() %>

Upvotes: 1

Related Questions