Tuesday
Feb242009
ASP.NET Dynamic Data - Display Custom Text in a Foreign Key DropDownList/ComboBox
Eric Phan |
Tuesday 24 February 2009 at 01:21 PM 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:
- Create a new property with the exact formatting that you want (DisplayName in this example)
- Add the ScaffoldColumn attribute to the property and set it to true
- 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.
The display column ‘DisplayName’ specified for the table ‘Stores’ does not exist.
tagged
ASP.NET Dynamic Data in
Development
ASP.NET Dynamic Data in
Development 
Reader Comments (7)
Hi Eric,
Slightly 'off topic' - I was checking out an article of yours about 'Dynamic Data' and then clicked on your 'photos' tab. I was amazed/surprised to see a Pomchi(?) and a shiver ran down my spine when I saw 'coco'. We have recently inherited a Pomchi, called, you guessed it, Coco. We're besotted with him. Pic at www.hollywoodgnats.blogspot.com
Ah in the cold light of day yours looks like a pure-bred Pom, beautiful. Great set of photos, I feel like I know him/her already! Thought I'd imagined 'Coco' until I moused over!
I'm trying the same, but it doesn't work.
[DisplayColumn("FullName", "FullName", false)]
[ScaffoldTable(true)]
[DisplayName("Anagrafica dipendenti")]
public partial class Anagr_Dip
{
[ScaffoldColumn(true)]
public string FullName
{
get
{
return string.Format("{0}-{1}-{2}", Cognome, Nome);
}
}
}
Hi, n00b here, where do i put this code?
I couldn't make it work.
The display column 'DisplayDate' specified for the table does not exist.
Using .Net Framework 4.0 BETA
I was having trouble getting this suggestion to work until I realized I had put the DisplayName property in the Metadata class instead of the base class. The compiler gets confused if you put it in the metadata because no column named DisplayName exists in the table. In my case, I'm working with an Opportunities table, and once I put the DisplayName property in the Opportunity class rather than Opportunity_Metadata, it worked fine.
Thanks!
This doesn't seem to work, as suggested somewhere else (I can't find link).
A solution that does appear to work is at
http://blogs.msdn.com/b/rickandy/archive/2008/11/22/improving-the-fk-field-display-showing-two-fields-in-foreign-key-columns-with-ef.aspx
Example that would create a foreign key dropdownlist where the datatextfield is made up of three fields.....
The key is to overide ToString()....
public partial class Contact
{
public override string ToString()
{
return NameLast.ToString() + ", " + NameFirst.ToString() + ", " + Address1 + ", " + AddressPostCode ;
}
}