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.
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.
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.
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
See my blog C# Bits | Twitter @sjnaughton Always seeking an elegant solution.
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.
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?
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
See my blog C# Bits | Twitter @sjnaughton Always seeking an elegant solution.
None
0 Points
9 Posts
Issue in display of foreign key column when Scaffolding is enabled
Mar 13, 2012 03:59 PM|nancy1985|LINK
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.
All-Star
17916 Points
5681 Posts
MVP
Re: Issue in display of foreign key column when Scaffolding is enabled
Mar 14, 2012 05:52 PM|sjnaughton|LINK
These foreign keys must be defined in the Model or the Database they cannot be infered.
Always seeking an elegant solution.
None
0 Points
9 Posts
Re: Issue in display of foreign key column when Scaffolding is enabled
Mar 15, 2012 11:32 PM|nancy1985|LINK
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.
None
0 Points
9 Posts
Re: Issue in display of foreign key column when Scaffolding is enabled
Mar 15, 2012 11:33 PM|nancy1985|LINK
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.
All-Star
17916 Points
5681 Posts
MVP
Re: Issue in display of foreign key column when Scaffolding is enabled
Mar 16, 2012 05:26 AM|sjnaughton|LINK
what data type is your Primary key and are there any text fields in the FK table?
Always seeking an elegant solution.
None
0 Points
9 Posts
Re: Issue in display of foreign key column when Scaffolding is enabled
Mar 20, 2012 12:26 PM|nancy1985|LINK
My primary key fields are integers. Most of them are autoincremented. Yes, I do have data in the foreign key table.
All-Star
17916 Points
5681 Posts
MVP
Re: Issue in display of foreign key column when Scaffolding is enabled
Mar 20, 2012 12:44 PM|sjnaughton|LINK
then there is somthing wrong with either your project or DB as this works out of the box with DD
Always seeking an elegant solution.
None
0 Points
9 Posts
Re: Issue in display of foreign key column when Scaffolding is enabled
Mar 20, 2012 02:34 PM|nancy1985|LINK
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.
All-Star
17916 Points
5681 Posts
MVP
Re: Issue in display of foreign key column when Scaffolding is enabled
Mar 20, 2012 02:43 PM|sjnaughton|LINK
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
Always seeking an elegant solution.
None
0 Points
9 Posts
Re: Issue in display of foreign key column when Scaffolding is enabled
Mar 20, 2012 03:06 PM|nancy1985|LINK
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.
All-Star
17916 Points
5681 Posts
MVP
Re: Issue in display of foreign key column when Scaffolding is enabled
Mar 20, 2012 03:14 PM|sjnaughton|LINK
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
Always seeking an elegant solution.
None
0 Points
9 Posts
Re: Issue in display of foreign key column when Scaffolding is enabled
Mar 20, 2012 03:20 PM|nancy1985|LINK
Could you please explain me in detail again? Where should this attribute be set?
None
0 Points
9 Posts
Re: Issue in display of foreign key column when Scaffolding is enabled
Mar 20, 2012 03:34 PM|nancy1985|LINK
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?
All-Star
17916 Points
5681 Posts
MVP
Re: Issue in display of foreign key column when Scaffolding is enabled
Mar 20, 2012 03:42 PM|sjnaughton|LINK
Always seeking an elegant solution.