locked
Why DisplayName doesn't work for Foreign Key fields? RRS feed

  • Question

  • User318430210 posted

    Hi.

    I'm using DisplayeName[("a name")] to change the display name of columns in Dynamic Data. It works fine for all the columns except for Foreign Keys. It doesn't chnage Foreign Key field's name and the display name of Foreign Table is shown instead. How can I fix this?

    Sunday, May 13, 2012 1:42 AM

Answers

  • User-330204900 posted

    Hi karadous, the thing about FK columns is they are not displayed what you need to use are the Navigations properties, for instalce you have an FK column in Northwind Order_Details of ProductId but the NavigationProperty will be called Product NOT ProductId. It's a mistake we have all made with DD :)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, May 13, 2012 2:33 PM
  • User350138131 posted

    I act this way, for example in Default EntityTemplate 

    if (currentColumn.GetAttributeOrDefault<DisplayAttribute>().Name == null)
    {
    	if (currentColumn.GetType() == typeof(MetaChildrenColumn))
    		label.Text =  ((MetaChildrenColumn)currentColumn).ChildTable.DisplayName;
    	else if (currentColumn.GetType() == typeof(MetaForeignKeyColumn))
    		label.Text = ((MetaForeignKeyColumn)currentColumn).ParentTable.DisplayColumn.DisplayName;
    	else
    		label.Text = currentColumn.DisplayName;
    }
    else
    	label.Text = currentColumn.DisplayName;


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 14, 2012 2:09 PM

All replies

  • User-330204900 posted

    Hi karadous, the thing about FK columns is they are not displayed what you need to use are the Navigations properties, for instalce you have an FK column in Northwind Order_Details of ProductId but the NavigationProperty will be called Product NOT ProductId. It's a mistake we have all made with DD :)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, May 13, 2012 2:33 PM
  • User350138131 posted

    I act this way, for example in Default EntityTemplate 

    if (currentColumn.GetAttributeOrDefault<DisplayAttribute>().Name == null)
    {
    	if (currentColumn.GetType() == typeof(MetaChildrenColumn))
    		label.Text =  ((MetaChildrenColumn)currentColumn).ChildTable.DisplayName;
    	else if (currentColumn.GetType() == typeof(MetaForeignKeyColumn))
    		label.Text = ((MetaForeignKeyColumn)currentColumn).ParentTable.DisplayColumn.DisplayName;
    	else
    		label.Text = currentColumn.DisplayName;
    }
    else
    	label.Text = currentColumn.DisplayName;


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 14, 2012 2:09 PM
  • User-330204900 posted

    Are any other attributes working?

    Tuesday, May 15, 2012 7:11 AM
  • User318430210 posted

    Yes. Other attributes work. Is there any way to change the name of foreign key field? For example I have a MangerID in my department table that points to PersonnelID in Personnel table. I want to show Manager for the display name of this field in List mode or Edit mode of Department table. But it shows Personnel.

    Sunday, May 20, 2012 9:37 AM
  • User-1488931086 posted

    Is there any way to change the name of foreign key field?

    Hard to say……I'm afraid no……:(

    Monday, May 21, 2012 4:21 AM
  • User-330204900 posted

    Hi TimoYang, the answer is yes you can change the display name for FK columns using

    [Display(Name="My Name")]

    the issue as always with FK columns is putting the attribute on the correct column it must go on the Navigation Property associated with the FK column as that is what DD displayes.

    Monday, May 21, 2012 4:32 AM