locked
Issue in display of foreign key column when Scaffolding is enabled RRS feed

  • Question

  • User1381821790 posted

    Hi,

    I am new to ASP.NET. I have created a website with Scaffolding enabled on all the tables of my database. I understand that the foreign keys are automatically detected and relations are established between tables.

    If any table has a foreign key, it is seen that in the List view of that table ,a link will be provided (in the place of the foreign key value) which opens the corresponding row in the foreign table. However the link value will not be same as the actual foreign key value ( I have observed that it picks the first non-null varchar column of the foreign table.)

    For Example, I have two tables:

    User with columns : UserID int, DepartmentID int (which is a foreign key column pointing to Department table) and

    Department with columns: DepartmentID int primary key,Status varchar not null ,Name varchar not null.

    In the list view of User table, under DepartmentID column, I can see that it displays the data in Status column corresponding to that DepartmentID.This is actually inappropriate and is confusing to the users.  Instead it should have displayed a link whose value is same as that of the departmentID. 

    The same issue is seen in the DropdownList of the foreign keys which which appear as filters and also in the Edit and Insert pages.

    Can some body please let me know how to change this? I want my foreign key column in the list view and also the foreign key dropdowns to have the actual value of the Foreign key and not the value of some other varchar column of the table.

    Tuesday, March 13, 2012 3:59 PM

Answers

  • User-330204900 posted

    I understand that this attribute should be set in my Model.Designer.cs, before the partial class declaration. How ever this will be overridden when the model is updated at some poing of time.. How do we handle it?

    you cannot add this to the designer file as this is autogenerated code you need abuddy class see 8. Using ASP.NET Dynamic Data

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 20, 2012 3:42 PM

All replies

  • User-330204900 posted

    These foreign keys must be defined in the Model or the Database they cannot be infered.

    Wednesday, March 14, 2012 5:52 PM
  • User1381821790 posted

    The foreign keys are already defined in the Database. I do see them in the model too. How ever when they are displayed in the dropdown list, the appropriate value is not displayed as I have already described in my question earlier.

    Thursday, March 15, 2012 11:32 PM
  • User1381821790 posted

    The foreign keys are already defined in the Database. I do see them in the model too. How ever when they are displayed in the dropdown list, the appropriate value is not displayed as I have already described in my question earlier.

    Thursday, March 15, 2012 11:33 PM
  • User-330204900 posted

    what data type is your Primary key and are there any text fields in the FK table?

    Friday, March 16, 2012 5:26 AM
  • User1381821790 posted

    My primary key fields are integers. Most of them are autoincremented. Yes, I do have data in the foreign key table.

    Tuesday, March 20, 2012 12:26 PM
  • User-330204900 posted

    then there is somthing wrong with either your project or DB as this works out of the box with DD

    Tuesday, March 20, 2012 12:44 PM
  • User1381821790 posted

    I think you have not got the question.

    I am not saying that the foreign key mapping is not working.

    It is just that the foreign key columns do not show the key field data.. I hope my question in the starting of the thread clearly explains that.

    Tuesday, March 20, 2012 2:34 PM
  • User-330204900 posted

    Sorry, I think you missunderstand DD does this FK dropdowns out of the box no work required, it only shows the FK value when a the column type is a string or there is no string column in the FK table, it just works, so if it os not working then I would look at your DB. Try using a standard DB like Northwind or AdventureWorks or even the Chinook Database

    Tuesday, March 20, 2012 2:43 PM
  • User1381821790 posted

    Ok.. I understand that the DD will display the foreign key value only if its a String or if the foreign key table does not have any string column. If not, it will pick the first string column of the foreign key table and display it (which is what is happening in my case) . Please correct me if I am wrong.

    Tuesday, March 20, 2012 3:06 PM
  • User-330204900 posted

    in that case you need to use the DisplayColumn attribute on the FK table to indicate which column to display it uses the format

    [DisplayColumn("ColumnToDisplay", "ColumnToSortBy", true)]

    if last parameter is set to true (it is optional) the sort applied os descending

    Tuesday, March 20, 2012 3:14 PM
  • User1381821790 posted

    Could you please explain me in detail again? Where should this attribute be set?

    Tuesday, March 20, 2012 3:20 PM
  • User1381821790 posted

    I understand that this attribute should be set in my Model.Designer.cs, before the partial class declaration. How ever this will be overridden when the model is updated at some poing of time.. How do we handle it?

    Tuesday, March 20, 2012 3:34 PM
  • User-330204900 posted

    I understand that this attribute should be set in my Model.Designer.cs, before the partial class declaration. How ever this will be overridden when the model is updated at some poing of time.. How do we handle it?

    you cannot add this to the designer file as this is autogenerated code you need abuddy class see 8. Using ASP.NET Dynamic Data

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 20, 2012 3:42 PM