Compartilhar via


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.

Comments

  • Anonymous
    November 21, 2008
    PingBack from http://mstechnews.info/2008/11/improving-the-fk-field-display-showing-two-fields-in-foreign-key-columns-with-ef/

  • Anonymous
    January 16, 2009
    Rick Anderson Rick’s blog focuses on Dynamic Data, including a FAQs and Dynamic Data samples. Most current

  • Anonymous
    January 26, 2009
    Please post corrections/new submissions to the Dynamic Data Forum . Put FAQ Submission/Correction in

  • Anonymous
    May 04, 2009
    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 { }

  • Anonymous
    August 04, 2009
    Great solution! But what if I would want to display 2 columns (Last and First name separately) instead of 1? Thank you

  • Anonymous
    November 06, 2009
    "ToString" works. But the problem is, how do you control the sorting then? Thanks

  • Anonymous
    December 01, 2010
    I have tried WesSmith's technique, and the technique of adding a public property to a partial class (as opposed to a public method in WesSmith's example) and neither work.  Only the very limited ToString() override seems to work. Ideas?

  • Anonymous
    February 05, 2012
    Hi Rick_Anderson Can you please tell me, in which file should I make the above changes? Please help me out. Thanks

  • Anonymous
    December 11, 2012
    No answer to odinhaus' question? I am having the same problem.

  • Anonymous
    January 30, 2014
    Hi I have Question in Dynamic Data. How can i display Fields(label and textbox ) instead of dropdown foreign key in dynamic data... so that i can insert  new record in this foreign key also  .. i have searching about this but i didn't find it's answer yet..