ASP.NET Dynamic Data - Display Custom Text in a Foreign Key DropDownList/ComboBox


I got asked a question recently about ASP.NET Dynamic Data and how to customize the display text of foreign key dropdownlists/comboboxes

The exact question was “I have a foreign key to a list of store locations. Each location has an address but the default drop down only shows the state. How do I get it to show: “State - Region - StoreName”

Here’s how you do it:

Create a partial class of your domain object and add a new column

[DisplayColumn("DisplayName", "DisplayName", false)]
public partial class Store
{
    [ScaffoldColumn(true)]
    public string DisplayName
    {
        get
        {
            return string.Format("{0}-{1}-{2}", State, Region, StoreName);
        }
    }
}

The key here is to:

  1. Create a new property with the exact formatting that you want (DisplayName in this example)
  2. Add the ScaffoldColumn attribute to the property and set it to true
  3. Add the DisplayColumn attribute to the class and use DisplayName

Note: if you don’t scaffold the column you will get an error like:

System.Web.DynamicData.MetaTable.get_DisplayColumn()
The display column ‘DisplayName’ specified for the table ‘Stores’ does not exist.

10 Comments