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 (10)

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

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

  3. WesSmith says:

    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 {

    }

  4. caio says:

    Great solution!

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

    Thank you

  5. ln613 says:

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

    Thanks

  6. odinhaus says:

    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?

  7. Sam Thapar says:

    Hi Rick_Anderson

    Can you please tell me, in which file should I make the above changes? Please help me out.

    Thanks

  8. Sonny says:

    No answer to odinhaus' question? I am having the same problem.

  9. Owais says:

    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..