Welcome to MSDN Blogs Sign in | Join | Help

Improving the FK field display: Showing two fields in Foreign Key columns

 

The default scaffold of the CustomerAddress table in the AdventureWorksLT database poses a problem: Dynamic Data (DD) defaults to using the first string field in the referenced table. In this case, the first string field is the Title field (Mr,Ms, and so on).  The image below shows the problem with the FilterRepeater drop down list and Customer column elements.

We can take a first step toward fixing this issue by creating and annotating a partial class for the Customer entity. The code below now displays the LastName field for the customer entity. The DisplayColumn attribute tells referring entities which column to use for display instead of the Foreign Key field.

 [DisplayColumn("LastName")]
 [MetadataType(typeof(CustomerMetaData))]
 public partial class Customer {
      public class CustomerMetaData {
   }
 }

While the resulting view is an improvement, it’s not there yet. Note that it doesn't distinguish between the customers with non-unique last names, such as Adams or Liu.

 

To fix this, we overload the ToString method for the Customer partial class as shown below:

// [DisplayColumn("LastName")]
[MetadataType(typeof(CustomerMetaData))]
public partial class Customer {

     public override string ToString() {
         return LastName.ToString() + ", " + FirstName.ToString();
     }

     public class CustomerMetaData {

}

The resulting display gets it right: the code now shows the correct field, and it distinguishes between David, Jinghao and Kevin Liu.

Special thanks to David Ebbo for recommending the ToString() approach and Phil Haack for granting me blog dibs.

Published Saturday, November 22, 2008 3:17 AM by ricka0
Filed under: ,

Comments

# Improving the FK field display: Showing two fields in Foreign Key columns with EF | MS Tech News

# ASP.NET User Education team member blogs

Friday, January 16, 2009 7:28 PM by ASP.NET User Education

Rick Anderson Rick’s blog focuses on Dynamic Data, including a FAQs and Dynamic Data samples. Most current

# Dynamic Data FAQ

Monday, January 26, 2009 7:25 PM by Ricka on Dynamic Data

Please post corrections/new submissions to the Dynamic Data Forum . Put FAQ Submission/Correction in

# re: Improving the FK field display: Showing two fields in Foreign Key columns

Monday, May 04, 2009 1:43 PM by WesSmith

Before I can across this post I tried another approach that works... Set the DisplayColumn attribute to a function that returns what you want displayed. For example:

[DisplayColumn("LastFirst")]

[MetadataType(typeof(CustomerMetaData))]

public partial class Customer {

    public string LastFirst() {

        return LastName.ToString() + ", " + FirstName.ToString();

    }

    public class CustomerMetaData {

}

# re: Improving the FK field display: Showing two fields in Foreign Key columns

Tuesday, August 04, 2009 11:33 AM by caio

Great solution!

But what if I would want to display 2 columns (Last and First name separately) instead of 1?

Thank you

# re: Improving the FK field display: Showing two fields in Foreign Key columns

Friday, November 06, 2009 5:04 PM by ln613

"ToString" works. But the problem is, how do you control the sorting then?

Thanks

Anonymous comments are disabled
 
Page view tracker