DynamicData – Changing Foreign-Key DropDownList display column and format

I have decided to keep posting about ASP.NET Dynamic Data as I learn more about it. I have already posted about creating an ad hoc data entry site. I will continue using the same data model as I explore Dynamic Data. My data model is given below:

Entity Model

Entity Model

The entityview names and the entityviewcolumn names must have bilingual support in Arabic and English. Therefore, the EntityView and EntityViewColumn tables are linked to the Resource table using the DisplayNameId column. Each resource can be identified by the primary key or by a unique combination of the Group and Name columns of the Resource table (since the Resource table also stores the global resources for our custom resource provider). For example, the resource for the Id property of the Default view of the Role entity can be identified by the combination of Role_View_Default in the Group column and Id in the Name column. The Default view record in the EntityView table itself will have a resource identified by Role_View as Group and Default as Name. The Name property of the Manager view of the Service entity can be identified by the combination of Service_View_Manager and the view itself will have Service_View for Group and Manager for Name. I hope that it is not too difficult to grok.

After creating the admin site, I started entering data into the tables. I immediately hit a problem with selecting the correct resource while adding an EntityViewColumn record.

Editing an EntityView record

As you can see, by default DynamicData was pulling the Name column of Resource table to populate the Resource DropDownList. This does not help as the Name column is not unique and I cannot know, in this case, which ‘Default’ in the list is the one corresponding to the ‘Role_View’ Group column representing a view of the Role entity. The same problem will exist when adding or editing an EntityViewColumn as I will not be able to tell which value (say, ‘Id’) belongs to which Group (Role_View_Default or Role_View_Manager).

The default display column for an entity when a foreign-key points to it can be set by using the DisplayColumnAttribute on the entity class or its corresponding MetaDataType class.

namespace DHACoreModel
{
    [MetadataType(typeof(Resource_MD))]
    public partial class Resource
    {
        [DisplayColumn("Group")]
        public class Resource_MD
        {

        }
    }
}

I extended my Resource class belonging to my EF4 model using a partial class. The MetaDataTypeAttribute indicates the metadata class associated with the data-model class in which you can specify metadata information in an elegant manner. The DisplayColumnAttribute on the Resource_MD metadata class specifies that the Group column should be used to populate the foreign-key dropdownlist instead of the name column.

Editing an EntityView record

Of course, that still does not help me select a resource correctly. I wanted the Group and the Name column to appear in the format Group/Name; that is, I wanted two columns to be involved in the generating the display item in the list.  The resource for the Id column of the Default view of Role entity would be shown as Role_View_Default/Id. This would help me identify resources easily.

The FieldTemplate for Foreign-key columns is given in the ForeignKey.ascx and ForeignKey_Edit.ascx. The ForeignKey_Edit.ascx is used for editing and inserting purposes. The code-behind showed that the dropdownlist was populated using the FieldTemplateUserControl.PopulateListControl(ListControl) method when the form was in Insert mode. I used .NET Reflector to explore the method in the System.Web.DynamicData.dll file and after drilling down learnt that for each record in the table pointed to by a foreign-key column, the following method is executed to display the corresponding item in the list:

public virtual string GetDisplayString(object row)
{
    if (row != null)
    {
        row = this.PreprocessRowObject(row);
        if (this.HasToStringOverride)
        {
            return row.ToString();
        }
        object propertyValue = DataBinder.GetPropertyValue(row,
                                        this.DisplayColumn.Name);
        if (propertyValue != null)
        {
            return propertyValue.ToString();
        }
    }
    return string.Empty;
}

Therefore, if the corresponding entity class has a ToString() override, that will be called to generate the display name for each item in the list. If not, the DisplayColumnAttribute will be used. Modifying my Resource partial class to add a ToString override:


namespace DHACoreModel
{
    [MetadataType(typeof(Resource_MD))]
    public partial class Resource
    {
        public override string ToString()
        {
            return this.Group + "/" + this.Name;
        }

        [DisplayColumn("Group")]
        public class Resource_MD
        {

        }
    }
}

Now I can select the resources correctly and easily:

Editing an EntityView record

To conclude, the display items in the dropdownlist of a foreign-key column can be set easily by overriding the ToString() method of the entity class corresponding to the table pointed to by the foreign-key column.

Advertisements

2 thoughts on “DynamicData – Changing Foreign-Key DropDownList display column and format

  1. Hi. How can I change Display Name of a foreign key field? [Display(“Name”)] doesn’t change the name.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s